Abstraction in Java | Abstract Java Example

Abstraction is a concept of hiding stuff from the world so as to not overwhelm people with minute details. Abstraction in Java is fairly similar. Hell, our body is one form of abstraction where the functionality and working are kept hidden under the layer of skin. Doesn’t it kind of gross us out when we have a chance encounter with our insides? My maternal aunt had fainted once when she saw a skeleton for the first time in a Science Museum.

You see the point?

Abstraction is everywhere around us. When people manufacture a car they keep the engine hidden so as to hide the complex minutiae of its functioning. Same holds true for that CPU you have next to you. Isn’t it covered in a nice cabinet to give an impression as if its one box that’s keeping your system up and running? Oh, you are using a laptop? The stuff that does things is hidden there as well.

abstraction meme joke hiding in plain sight

So basically everything boils down to the following adage:

Don’t worry about how it does it, just think about what it does!

Got the idea? Let’s delve into what abstraction in Java is all about.

Abstraction in Java

Likewise, when you are hiding implementation details from the user it is known as abstraction. The user should only know about the functionality, and that’s it.

Abstraction in Java can be achieved by using abstract classes and Interfaces. The latter is a topic we will save for the next chapter.

So with that hiding logic, it becomes only logical to learn about an Abstract method first.

What is an Abstract Method?

When you are hiding the detailing of what your method does, it is an abstract method. An abstract method is not going to have any implementations, and will be declared as is. One good example would be:

abstract void doSomething();

Notice how the method is empty? It has been declared, yes. You have to precede the method with the keyword abstract, to tell the JVM that the method is an abstract one. Instead of curly braces here, the end should have a semi-colon instead.

Then, you might think what’s the point really?

An abstract method can be defined as per your usage in a subclass later. To a class that is abstract, it would give an impression of tidiness and hence people prefer using abstract methods and classes.

NOTE: Any class that is inheriting the current abstract class must override the abstract methods or declare itself as abstract again.

So in short, the subclass must implement the abstract method or you will keep having a hierarchy of abstract classes.

What is an Abstract Class? Java Abstraction

An abstract class is a class that is declared abstract using the abstract keyword but it may or may not have abstract methods inside.

But if a class has at least one abstract method, it has to be made abstract.

An abstract class cannot be instantiated. The only option you have is to use it as a superclass. Hope you remember the inheritance lesson.

If you are inheriting an abstract class, you need to provide the implementation of all abstract methods in it.

Here is one example related to Titanic to make the world a better place to live in:

abstract class with an abstract method

Now we need a subclass that extends this class. Now in that class, the method needs to be defined:

a class extends an abstract class example

Here the first thing to do is to use the “extends” keyword to tell the compiler that the abstract Class has been extended. The next step is, of course, to provide an implementation for the abstract method. You don’t have to use the abstract keyword here since you are providing implementation it is no longer an abstract method.

Running the above will give you the result:

You jump, I jump!

Remember it isn’t necessary for an abstract class to have all methods as abstract.

If you try to instantiate the abstract class, it will give you an error:

error while trying to instantiate abstract class

You can make it instantiate the subclass instead. So writing this wouldn’t be incorrect:

instantiating the subclass is possible

Running the program now will give you Jack’s reply too:

You jump, I jump!
You jump, I jump!

Concrete Class

Just like there is an abstract class that is good at hiding stuff, we have the opposite – concrete class. All the classes that we have been using so far in inheritance, where we were providing implementations, were all concrete classes.

That being said, the definition of a concrete class would be that it is a class that provides implementations to all its members that it inherits from either an abstract class or an interface.

If we take a look at the following example:

abstract class Boss {

                abstract public void shouting();

}

class Employee extends Boss {

            void shouting() {

                  System.out.println("Woah!");

           }

}

Employee class would be called a concrete class since it’s been providing implementations to all the Boss class methods. While the Boss class is busy providing abstraction in Java.

Why Abstract Class cannot be instantiated in Java?

So, you might be wondering why abstract class cannot be instantiated in Java? The reason being abstract classes are fuzzy and generalistic. They do not tell you the specifics that you need while creating an object. So since you cannot be certain of what is supposed to be instantiated there is no point in instantiating it.

Hope that’s clear!

As to how an abstract class can be used to provide an implementation to an interface we are going to see that in interfaces chapter.

Scottshak

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

You may also like...

3 Responses

  1. May 25, 2017

    […] are more or less based on the concept of Abstract Classes and are introduced to get rid of the multiple-inheritance deficiency. Meaning that multiple […]

  2. June 5, 2017

    […] is almost like Abstraction where we try to hide crucial information from our […]

  3. June 14, 2017

    […] Learn about concrete class. […]

Leave a Reply