Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract Class Vs Class

 
Rajarsi Ghosh
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the basic difference between abstract class and an ordinary class in Java.
What is the difference between an abstract class with all the methods defined and that of an ordinary class?
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Bryan Incognito
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajarsi-

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.
 
Ramen Chatterjee
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Hope this helps

Ramen
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic