• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

need of abstract class in java

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here some code about what I have tried to explain above:
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
siva prakashmahendran
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for all replied me this one is very usefull to me ...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic