Abstract Class In JAVA

Hello Friends,

This tutorial is for all the Java followers. One of the best feature that is widely used is the term ‘Abstract’. This term can be used as either class or a simple method.

An abstract method is any method that is just declared but not instantiated. In other words one can just create the declaration of the method but cannot write any thing in it to perform at that particular instance. And the class containing even a single abstract method then the class is called as ‘Abstract Class’‘ . To use that particular method one must have to inherit the class ( ‘abstract class’) then it has to be overridden as per required.

Ex:-

The above example shows the actual definition of the abstract class and method.As well as an abstract class may or may not have a simple method. In this case we have considered one normal method along with two abstract method.

How to use these abstract methods?

Since we cannot create the instance of class that contains these abstract methods there may be question in your mind that how to use the abstract method.

Think…

Think…

Yes Exactly…. You are right..

So what if we cant create the instance of this class, Java allows us to extend the class (Inheritance  my friend !!! ). We can extend the class and then (as we know properties of inheritance) override those methods.

But take note that once you extend the abstract class then you need to override all the abstract methods that are in the abstract class. Or else the class which is extending this abstract class it will also become a abstract class. Here in this case if class Program does not extend any of the abstract method then class Program will also be a Abstract class.

Why Abstrct Class?

Now one must think that why there is need of using abstract methods when we can directly write the normal method.

You are exactly right my friend, why there is need of using abstrct classes when we can have our normal classes with normal methods.

Now consider that I am your lead and I provided you an issue or say project where you have to write certain code for any particular purpose. And being a lead I want five of my methods to be included all time. So that all of them are included each time without any excuse. In this case would it be good that I have to tell you that look you have this this method and you have to create this method.

Ok fine. But what if you forget any of the method to use and your project is done. But now I cant see that method and I want you to include that at any cost. Then how would you feel that time to again rewrite the code.

So to avoid this confusion I would create one abstract class where I have all my five abstract method. And I will just tell  you to extend this abstract class. This will not allow you to move further with your code unless you override those methods.

I think you got your answer!!!!!!!.

Why Abstract class when we have Interfaces?

Ok. Now you need to know why we must use abstract class when we have interfaces.

The answer is relatively same as explained above, but still we will make it clear.

We consider an interface that has three methods – OpenDatabase(); CloseDatabase(); and ModifyDatabase(); and I want all this methods to be in the project. Now you can implement this interface to override these methods. That is you have fulfilled my need of  using those methods.

Now tell me what if when this methods are called in wrong order i.e. first open() is called then close() is called and then modify() is called. But this is not what I want. We know that when we deal with database, then it must be done in particular order i.e. to open first and then modify and then close it. but in this case my data will not be modified since DB is closed before being modified. So we have to write a simple method that will force the particular order of execution of the methods. But it is true that we cannot write a simple method in an interface, since it can only have abstract methods.

So how can we do this.

Come on my friend you know this…

Exactly. Using Abstract class. As we can write the normal method, in abstract class,along with abstract methods. We can provide the sequence of our choice in what manner which function should be called. And after that we just have to call that method in the main function and our work is done.

Hope this will help you understand the concept of Abstract class and its advantage over Interfaces.

Thank You,

Regards,

admin@code2java.com

2 thoughts on “Abstract Class In JAVA”

  1. Brilliant! Thanks for bringing these stratospheric architectural pretentsions down to earth, where we can kick them around a little

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.