Learn Abstraction In Java

Hey guys!! How are you doing!! Today we will learn abstraction in java.

In this post we are going to discuss about abstraction in object oriented programming structure.

Let’s start with definition,

Learn abstraction in java : what is abstraction?

Abstraction means hiding the implementation details and present the features to the user. Abstract classes are classes which are incomplete.

For example,

abstract class Vehicle
   String horn;
   abstract void printDetails();

class Car extends Vehicle
   String brand = "Audi";
   public void printDetails()

Let’s analyze above example,

I have made a class vehicle and variable string noise.

abstract void printDetails();

Notice something after the method name and after the parentheses there is a semicolon. I have not put method body.

There is no opening parenthesis, no closing parenthesis and no code inside that method.

But i have simply returned abstract to indicate that this method printDetails() is incomplete.

Now class car extends vehicle. Now when a class extends abstract class it becomes its responsibility to complete what was incomplete.

In other words printDetails() is incomplete in the superclass and the subclass has to make some code for that printDetails() method.

That’s the whole idea behind abstract classes. Now the question arises why would we make it incomplete in the first place and then ask us to complete it and what is the objective behind doing something like that??

I will take this topic shortly. Because first we gotta get the word abstract clearly.

So we can make an object of car and printDetails() method that can be called,

Car c = new Car();
c.brand = "Audi";

But you cannot make an apt object of abstract class vehicle. Because it is abstract, it is incomplete and you cannot make incomplete object.

Now let’s take a look at the rules of the abstract classes and then we will figure out how and why we should use them,

Rules for abstract class

  • Abstract classes are incomplete. Derived classes must declare the missing pieces to become complete classes, from which we can instantiate objects.
  • We can make more than one method as an abstract method.
  • Abstract class purpose is to provide an appropriate superclass from which all other classes can inherit and thus share a common prototype.
  • A class is said to be abstract if one or more methods are abstract.
  • Cannot create objects of an abstract class.
  • Derived classes must provide an implementation of the abstract methods of the superclass or themselves be declared abstract.
  • Abstract class can still have a variable whose type is an abstract class. The actual object to which it refers must be an instance of a concrete subclass.
  • Abstract class can have instance variables and they can have concrete methods.
  • Abstract superclass names can be used to invoke static methods declared in those abstract superclasses.
  • Constructors and static methods cannot be declared abstract.

Learn abstraction in java : why should we use abstract class?

Now let’s understand how and why we should use abstract class.

Remember, we had a question saying why would we make incomplete class?? Let’s take a look at that below with the help of example,

Learn Abstraction In Java

Let’s assume a guy, Sam who meets a software programmer. Now sam has four outlets and he wants programmer to write software that helps him in managing all the four outlets.

For simplicity let’s assume that all sam wants to do is to calculate profits from each outlet every month.

He owns a grocery outlet, stationery outlet and some other two types of outlets.

So here the question is will programmer write four different softwares for working with four outlets or write a single piece of software that works with all the four outlets.

So the basic operation to do is let’s assume to calculate the profit from all the four outlets.

Now the programmer writes code like this,

abstract Outlet

abstract totalProfit()

Here totalProfit() will obviously add all the items that the outlet has. What is there purchase value and how many items were sold and what is there selling value and then it would somehow calculate profit between them.

Here i’m not getting into details of the totalProfit() method rather let’s focus on how each shop is going to work with abstract outlet class.

So the first outlet is going to extend abstract Outlet class.

We can write like this,

Outlet 1

abstract totalProfit()

totalProfit() of outlet 1 has to override abstract totalProfit() of abstract outlet class.

Because this is an abstract class and when you extend an abstract class you are supposed to give a definition to the method which is defined by the abstract class as abstract.

So outlet 2 also does the same thing extend abstract class. Outlet 3 repeats and outlet 4 does the same thing.

Now take a look at how uniform they are. totalProfit() is the same method name used in all the four outlets rather what changes is how the profits are calculated.

Outlet 1 maybe selling groceries. The way of calculating profit depend on the price of vegetables and other items. Outlet 2 maybe selling stationery.

There the profit depends on the items that are being sold like books or papers or other stuff that outlet has.

Outlet 3 and outlet 4 have the same way. Now the whole idea behind making the shop as abstract was to give a uniform approach by having the same method totalProfit() being implemented in different ways in a slightly different way at all the other outlets.

Now outlet access the superclass for everyone and calculated totalProfit() method is kind of like a way of identifying what is common among all the outlets.

Now this looks similar to polymorphism stuff that we learnt in the previous post. Abstract classes are just another way of showing polymorphism in java.


That’s it guys. This was all about learn abstraction in java. I hope you have understood the concept.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this article if you like.

Related Posts