aspose file tools
The moose likes Java in General and the fly likes need of abstract class in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Badass: Making Users Awesome this week in the Game Development forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "need of abstract class in java " Watch "need of abstract class in java " New topic

need of abstract class in java

siva prakashmahendran

Joined: May 03, 2010
Posts: 12
surfing all related java sites and forum I cant able to get the answer for
when we going for abstract class ?
Please help me out ....

thanks in advance
Soumyajit Hazra
Ranch Hand

Joined: Jun 26, 2007
Posts: 136
One use I have seen to enforce template design pattern. We can fix the sequence of method call using abstract class.
Suppose we have a abstract class like the following is present

Now if we write classes which extends the MySeq class we have to override the printBody() and then if we call the printPage() always a proper sequence of method will execute, which first print the head then body(which will different in different subclass) and then the footer.
So we don't have to maintain the sequence i.e. multiple times call to printHead()/printFoot() will be avoided. That means we are standardizing the subclasses .
There may be other uses but this one I used mainly.

Java Programmer | SCJP 1.5 | SCWCD 1.4
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Why not look at some of the abstract classes in the standard packages and see why they are abstract. The java.util.Abstract* classes would be a good example.
Guido Granobles

Joined: Sep 11, 2010
Posts: 22
You might want to use an abstract class when you wish that all the child classes define some default implementations of the methods of the super class. Let's suppose that you are working as a member of a programming team and you have wrote a Car class. The Car class has methods for init the engine, stop the engine, press the clutch among others. Now suppose that you have other class which deal with objects from the Car class. Let's name this class CarProbe. The CarProbe class has the initProbe method among others. This method accept a Car object as parameter. This method must init the engine, press the clutch among other things. So the initProbe method will be invoking all these methods from the Car object. But there is a lot of types of cars like Mazda, Audi, Renault, BMW, Mercedez... and suppose that each one of them have a different way to init the engine, press the clutch (if it has one). So if the Car class is not an abstract class. Every child class of it could define a method for init the engine with any name for example: initEgineMazda(), initEgAudi(), BMWInit(). In this way the initProbe method won't know what method to call as it does not know what type of car it is dealing with. Then you will need write one initProbe method for every type of car that exist. Every initProbe method will accept a different type of car and will call the appropiate method for init the engine. But remember you are working as a member of the programming team and you are in charge of the Car class and the CarProbe class. The child classes will be written by other programmers and this programmers will be calling the methods from the CarProbe class. Maybe one of those programmers have a class Porsche and your CarProbe class has not consider this type of car. In fact every time that a programmer write a new class for other type of car you will add a new methods to the CarProbe class. That is a big problem. Here is when you want to use an abstract class. If the Car class is an abstract class and it declares a method named initEngine then all child classes must implement the method initEngine. So if the initProbe method accept a Car object as parameter. It knows that it can invoke the initEngine method always regardless what type of car has been passed to the method. Now it does not matter how many type of cars the other programmers are going to create as long as those type of cars implement all the methods form their super class.
Guido Granobles

Joined: Sep 11, 2010
Posts: 22
Here some code about what I have tried to explain above:
K Abhijit
Ranch Hand

Joined: Mar 03, 2008
Posts: 88
Abstract classes can be used to to give Semi Open behaviors for Components...

IT has ability to

1. Enforce : few API implementation from Clients (abstract methods needs to have Implementation in the derived class else child class would not be Concrete and can not be Constrcuted )

this enforcement you CAN NOT achieve with REGULAR NON ABSTRACT JAVA CLASS

2. Restrict overriding few API implementation. Give Complete implementation for few methods and mark them FINAL
this restriction you CAN NOT achieve with INTERFACES

“The difference between 'involvement' and 'commitment' is like an eggs-and-ham breakfast: the chicken was 'involved' - the pig was 'committed'.”
siva prakashmahendran

Joined: May 03, 2010
Posts: 12
Thank you very much for all replied me this one is very usefull to me ...
I agree. Here's the link:
subject: need of abstract class in java