This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
What is the basic difference between abstract class and an ordinary class in Java. The keyword "abstract" in class or method declaration. Other ordinary subclasses need to implement these abstract methods.
What is the difference between an abstract class with all the methods defined and that of an ordinary class?
Abstract classes can not be instantiated. Any attempt to instantite an abstract class, the compile time error occurs. [ May 20, 2006: Message edited by: wise owen ]
I would like to add to what Wise Owen said; perhaps it would help you to see a situation.
An abstract class cannot be instantiated. An abstract method MUST be overridden by subclasses.
This implies the following: (I'm keeping it simple, and making the inheritance tree only 2 levels deep)
-- If you have an abstract class, it has to be subclassed (that is, extended) in order to be instantiated. -- If your abstract class has abstract methods, then those methods have to be implemented in your subclass -- If your abstract class has all of its methods defined, then it means that writing your subclass is really easy, because all those methods in the superclass are already implemented for you.
Can you see how this works?
A note about my simplification: you can subclass an abstract class, and make your subclass also abstract, and thus it would not have to define the abstract methods. But the first non-abstract class down the tree will have to do so; the methods in the first non-abstract class will be inherited in turn, by its subclasses.
Just a quick addition to the above posts regarding use.
Assuming you are comfortable with inheritance, that is if I subclass a class (extend), then I inherit the behaviour of the super class. I can also override the behaviour of the superclass if I so choose.
When you create an abstract class you are saying to any descendants that you have some behaviour that you can already define, but there are some methods that may have differing implementations. As the creator of the super class I dont know what that behaviour is yet, but I know you must implement it.
Those metohds are declared abstract. Now anyone extending the class is forced to provide an implementation, even if it is an emtpy method. If your class has all abstract methods, it might suggest an interface.