Friday, December 12, 2008

Two silly questions for abstract classes

1) In which case we need : An abstract class with no abstract methods....

Case 1: I have seen many projects using the base class declared as Abstract. Example : Base class for DVOs, Action classes, DAOs etc. These class can contain some basic methods and properties which can be used or overridden by the subclasses. Here, the main purpose of the class is to create a structure (inheritance) not creating objects.

Case 2: Some adapter classes for AWT listeners are abstract with no abstract methods. For example java.awt.event.WindowAdapter . It represents the scenario when we dont want to implement all the methods of an interface. The subclass can override the methods for a particular functionality.

Case 3: In some cases, we need a class that should not be instantiated and the sub class should use or override some of the methods.

Example : javax.servlet.http.HttpServlet

___________________________________________________________

2) In which case we need : A class with all methods defined as abstract (or should we go for Interface always in this case)

Lets create the problem now :)

7 comments:

Vivek Athalye said...

looks like no1 else is interested in answering the questions :(

so here i am:

1) in case u r declaring few classes which are pure DVOs with no business logic whatsoever, and you find that they have few properties common (say: id, name, description). then u can declare an abstract parent class having the common properties and getter / setter methods. i don't see any other case when u might need an abstract class without any abstract method.

2) if u want to declare some non-final (and non-private) properties that are common to the child classes then in that case u can't define an interface. instead u'll need to have an abstract class with those properties defined and all methods declared abstract.

does it make any sense?

nickp2 said...

why would you declare a class to be abstract if you have no abstract method in it ? You might as well declare it as a normal class and other people will extend from it...

Tonnet said...

Thats why I called it a silly question. I just want to confirm if there is a design scenario when we need such a wonderful class :)

VSonet said...

Abstract classes can contain fields that are not static and final, and they can contain implemented methods. Such abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation. If an abstract class contains only abstract method declarations, it should be declared as an interface instead.

Multiple interfaces can be implemented by classes anywhere in the class hierarchy, whether or not they are related to one another in any way. Think of Comparable or Cloneable, for example.

By comparison, abstract classes are most commonly subclassed to share pieces of implementation. A single abstract class is subclassed by similar classes that have a lot in common (the implemented parts of the abstract class), but also have some differences (the abstract methods).

Tonnet said...

Updated the post....

Vivek Athalye said...

Good to know abt WindowAdapter. I never noticed its an abstract class. For that matter all the Adapter classes are abstract.

So we can conclude that in case of Adapter its always going to be an abstract class with no abstract method.

That was a good example 3bhu1 :)

Tonnet said...

Updated post