wood burning stoves*
The moose likes Beginning Java and the fly likes About Abstract Class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About Abstract Class?" Watch "About Abstract Class?" New topic
Author

About Abstract Class?

Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
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


Anto Telvin Mathew<br />Many of the life failures are people who did not realize how close they were to success when they give up. EDISON
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Oct 27, 2005
Posts: 19682
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
thanks
 
jQuery in Action, 2nd edition
 
subject: About Abstract Class?