I understand that if there are one or many abstract methods in a class then we must declare the class as abstract. I also read that an abstract class MAY contain one or more abstract methods. So it means, we can have an abstract class without any abstract method. I'm not able to understand the purpose of an abstract class without any abstract method. Any pointers ?
hi Swapnil Deshpande I think that you may know if anyone tries to make an object of an abstract class, the compiler prevents them in order to enforce a particular design.
When the abstract class is inherited, abstract method must be implemented. As we know, abstract method is useful for part of classes which extend the abstract class, it may be useless for another's part of classes but belong to the same group(identify in the design and may require to communicate using Polymorphism). How can you reduce some workload for coding? Lazy is a good reason for me not to create any abstract method in an abstract class.
At first it's not clear why you would ever want to make a class abstract when it has no abstract methods. If all the methods are abstract, you'd use an interface; if some methods are abstract and others are not, you're not given a choice; however, if no methods are abstract then why make the class abstract at all? Why not just make it a concrete class and allow it to be extended in the normal way, and its methods overridden as required?
The only reason I can think of where this practice makes sense is where the default implementations of the methods in a class provide no useful functionality by themselves, i.e. the class MUST be extended to be of any use. There are examples of this in the Java 2 Platform.
Consider the java.awt.event.WindowAdapter class: it implements 3 event listener interfaces providing empty implementations of each interface method. The class contains no abstract methods but would be useless if instantiated on its own. The designer/developer therefore declares the class abstract to indicate that it's not intended to be instantiated.
The java.awt.event.WindowAdapter class is provided as a convenience class such that classes wishing to use only one or two methods from the event listener interfaces don't have to create empty implementations of the other interface methods themselves. A concrete class would just extend it and override those methods required. Neat, huh?
I hope that explanation serves to illustrate the value of this rather peculiar type of abstract class.
[ August 15, 2004: Message edited by: Julian Kennedy ] [ August 15, 2004: Message edited by: Julian Kennedy ]
In my current project, I created an AbstractWacsfabException class which is the root of all application-specific exceptions. Although it doesn't have any abstract methods, I did make it an abstract class because I want to force my team to create subclasses that are more descriptive of the problem that has occurred (e.g. AccountExpiredException extends AccountException extends AbstractWacsfabException).