File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Super class constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Super class constructors" Watch "Super class constructors" New topic
Author

Super class constructors

Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 217

Hi all,
why do we have to call the super class constructors in order to use the instance variables and methods of sub class.Even if we want to use the instance variables inside the constructor of the sub class,we cannot do so unless we have the constructors of all the super classes run already.
Why is so?
Why is it necessary for the sub class to run the constructors of super classes to use its own instance variables inside its own constructor?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Because a sub-class-object IS A super-class-object. that is the basis of inheritance.A car which is a green Ford also IS-A vehicle which is a green Ford. So when you create your object as a green Ford car, you have to create it as a green Ford vehicle first.
Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 217

Thanks
 
Consider Paul's rocket mass heater.
 
subject: Super class constructors