Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Abstract Class?

 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
i know that every class we created has a default constructor .what about the case of abstract class.is abstract class also contains one.if so when it will get invoked because we can't instantiate that
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's easy to put together a small program that will test this very question. Why not try this and come back with your results?
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anto telvin Mathew:
i know that every class we created has a default constructor .

No it doesn't. Just look at java.lang.Integer.
A default constructor is only added by the compiler if there are no other constructors in a class.

what about the case of abstract class.is abstract class also contains one.if so when it will get invoked because we can't instantiate that

Every constructor will call either another constructor in the same class (using this(...)) or a constructor in its parent class (using super(...)). If you as a programmer define neither, the compiler adds "super();" as the first line of your constructor.

Concrete classes that extend an abstract class are no exception. Therefore, the abstract class must have at least one constructor. If it wouldn't have the concrete classes couldn't call it and those classes wouldn't compile.

You can simulate the latter by creating a class with only private constructors. These are unavailable for all other classes including sub classes. Trying to create such a sub class will fail.
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic