This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructors

 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does the constructors of the base class gets invoked while creating objects for the sub class??pleas explain
 
Sachin Joshi
Ranch Hand
Posts: 83
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"SachinJoshi Joshi",

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

Thank you,

Rob
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic