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
Joined: Jun 26, 2007
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.
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.
Joined: Sep 11, 2010
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.