why does any one want to have an abstract class with concrete methods only,(NO abstract methods) if one cannot instantiate the class. Moreover, why does any one want to put concrete methods in an abstract class and extend it in order to use them.(Remember, the class contains concrete methods only and NOT abstract methods).
I think the question is more like... why wouldn't they? First of all, if a class ONLY has static methods, you aren't going to want to instantiate it anyway. What would be the point in doing so, when you can already use all of the methods externally? As for inheritence... an abstract concept like Food might have a "bite" method. There is pretty only one way to bite (yes, I know that can be debated, this is just an example), so if Apple and Orange inherited the bite() method from your abstract Food class, they would need to be the same. And of course, Food is abstract because there has never been an instance of food, just types of food. [ July 01, 2004: Message edited by: Darin Niard ]
Dear Raghu, One of the good examples of your answer is adapter classes in Java2. Like MouseAdapter. It has all the methods defined(though empty) but still is an abstract class. And the reason is simple that sun wants us not to create the object of the class as it is.
Thanx everyone, specifically to Sandeep, for pointing me to the MouseAdapter class.I am including the explaination at SUN site ,in case if any others want to be more clear on this topic. :-) A interviewer really screwed me up, with this question. ----------- public abstract class MouseAdapter extends Object implements MouseListener
An abstract adapter class for receiving mouse events. The methods in this class are empty. This class exists as convenience for creating listener objects.
Mouse events let you track when a mouse is pressed, released, clicked, when it enters a component, and when it exits. (To track mouse moves and mouse drags, use the MouseMotionAdapter.)
Extend this class to create a MouseEvent listener and override the methods for the events of interest.
(If you implement the MouseListener interface, you have to define all of the methods in it. This abstract class defines null methods for them all, so you can only have to define methods for events you care about.)
Create a listener object using the extended class and then register it with a component using the component's addMouseListener method. When a mouse button is pressed, released, or clicked (pressed and released), or when the mouse cursor enters or exits the component, the relevant method in the listener object is invoked and the MouseEvent is passed to it. ---------- Regards, Raghu
You may find abstract classes with non-empty methods, too. We have an abstract command class that has a bunch of convenience methods. It's not a complete class because the main execute() method IS empty so it should not be instantiated and used as is. This is a common technique for "framework" type things.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi