• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Abstract Class Vs Class

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic