Hi Arul, Thats a good question. A class can be declared abstract even if it does not actually have any abstract methods. Declaring such a class abstract indicates that the implementation is somehow incomplete and is meant to serve as a superclass for one or more subclasses that will complete the implementation. Such a class cannot be instantiated.
Joined: Oct 13, 2003
Thanks for trying on my Q.
Declaring such a class abstract indicates that the implementation is somehow "incomplete"
What is a possible case for incompletion of an abstract class which does not have abstract methods?
Am asking in real implementation perspective. [ July 28, 2005: Message edited by: Arulkumar Gopalan ]
Originally posted by Arulkumar Gopalan: # We can have an abstract class, which does not have an abstract method.
Q: What is the purpose of having an abstract class with out having any abstract methods?
Any answer other than "to avoid creating instance for that class" is appreciated.
[ July 28, 2005: Message edited by: Arulkumar Gopalan ]
The only answer is "To avoid creating instance for that class". There is no other purpose.
SCEA, SCBCD, SCJP
Joined: Oct 13, 2003
Actually, I was not convinced that abstract should be used for that, instead of achieving it's original concept of polymorphism.
But, I understand now, that("To avoid creating instance of a class") could be the only answer from the below explanation.
Sometimes, a class that you define represents an abstract concept and, as such, should not be instantiated. Take, for example, food. Have you ever seen an instance of food? Probably not. What you see instead are instances of carrot, apple, and chocolate chip cookies. Food represents the abstract concept of what we can eat. It doesn't make sense for an instance of food to exist.
Having said abstract key word is used only for "to avoid creating instance of a class". But, below explanation says, that should be achieved using private constructor, not through abstract.
A class type should be declared abstract only if the intent is that subclasses can be created to complete the implementation. If the intent is simply to prevent instantiation of a class, the proper way to express this is to declare a constructor (�8.8.10) of no arguments, make it private, never invoke it, and declare no other constructors. A class of this form usually contains class methods and variables. The class Math is an example of a class that cannot be instantiated; its declaration looks like this: