This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why use abstract class?

 
Jerome Laggad
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please explain to me why use abstract class....
 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to "Java in General (beginner)"
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract classes are often used in frameworks to give you a partial implementation. They provide fully functional code in some methods, but leave other methods empty for you to override in a derived class. Or they might provide a very basic implementation of every method with the invitation to override any that you'd like to make more interesting. Swing has some in that style - like a default mouse event listener that implements every method to do nothing. You can extend it and override only the methods you really care about.
The other thing is the "abstract" bit, which means nobody can ever do a new() to create an instance of the class. That's nice when it is a partial implementation, because it's not complete enough for anyone to use.
When you get into some rather advanced situations it may be valuable to follow a rule "never extend a concrete class". In those cases it's good to give other programmers an abstract class that they may extend.
 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are especially helpful when you are in a team environment and you are responsible for modeling some aspect of the program which other developers may use. By laying the foundation, so to speak, with an abstract class, you define how you're model is to be used.
For instance, I've recently worked on a project where we had 3 types of users -- regular employees, supervisors, and reviewers. We made an abstract class Employee, which served as a superclass to each type of user. Now, in order to extend the Employee class, developers needed to override several key database methods (which were particular to a given type of user). The abstract Employee class let the other developers know what exactly they needed to implement in the subclasses.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic