aspose file tools*
The moose likes Beginning Java and the fly likes constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructors" Watch "constructors" New topic
Author

constructors

venkatesh badrinathan
Ranch Hand

Joined: Aug 03, 2008
Posts: 77
Why does the constructors of the base class gets invoked while creating objects for the sub class??pleas explain


SCJP1.5
Sachin Joshi
Ranch Hand

Joined: Aug 06, 2008
Posts: 83

A derived class is type of its super class. Therefore while creating an object of derived class details of constructing derived class are present in its own constructor but details specific to the super class are present in the super class only.

For example if you have a
Class MySuper {
int a;
public MySuper() {
this.a= 100;
}
}


Now if you write a new derived class for above class, you should always get the int a initialized to 100 for all objects. And that is possible only if the Super class constructor is called for each object of all subclasses. If this does not happen the purpose of inheritance is defeated.

This was a simple example but if you think about a super class which has many attributes and all of them to be inherited to its sub classes then it would make sense.


Web Development Tips and Tutorials - By Sachin
venkatesh badrinathan
Ranch Hand

Joined: Aug 03, 2008
Posts: 77
Thanks sachin, but
'All variables / fields that are declared in the construcotr block or the method block or a static block is not allowed to be accessed outside.
Include Private members too in that list.
When you say extends we extend the access to the behavior's(methods) of the class and nothing else'.

are not the above statements correct? I guess if they are correct, whether the following statement justifies?
'if you write a new derived class for above class, you should always get the int a initialized to 100 for all objects. And that is possible only if the Super class constructor is called for each object of all subclasses'
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

A constructor has the very specific job of getting an object ready for use. There is no reason for its internal workings to be used in any other way.

Instantiation of an object starts at the top of the hierarchy (with java.lang.Object) and works its way down.

For example, we might say that a Volkswagen IS-A Car, and a Car IS-AN Object. So if we're going to create a Volkswagen object, we start by creating an Object first (calling Object's constructor). Next, we take our Object and "extend" it into a Car (calling Car's constructor). And finally, we take our Car and "extend" it into a Volkswagen (calling Volkswagen's constructor).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19799
    
  20

"SachinJoshi Joshi",

I sent you a private message two days ago. I urge you to read it, and follow its instructions.

Thank you,

Rob


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructors