• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling constructors of inherited classes

 
Stephen Adams
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If I have three classes, called Class1, Class2 and Class3, and Class2 extends Class1 and Class3 extends Class2. If I want to use a method defined in Class1 in Class3, do I have to call the constructor of Class1 itself or will calling the contructor of Class2, which create an instance of Class2, that itself calls Class1 constructor so that an instance of Class1 is made and then I can use method1 which is defined in it.
Or is there a better way to call methods from top level classes?
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephen,
Welcome to JavaRanch.
If I want to use a method defined in Class1 in Class3, do I have to call the constructor of Class1 itself or will calling the contructor of Class2, which create an instance of Class2, that itself calls Class1 constructor so that an instance of Class1 is made and then I can use method1 which is defined in it.
If the method in Class1 is protected or public, or default (package private) and Class1 and Class3 are in the same package, then you can simply call the method directly from a Class3 object. It's important to understand what happens when you say:

A lot of things happen behind the scenes when you construct an object. In the scenario you listed above (and assuming you don't call an explicit super constructor in any of the constructors) when the Class3 constructor is called, the first thing it does is call the default (no args) constructor on Class2 which in turn calls the default constructor on Class1 which in turn calls the default constructor on Object (assuming that Class1 does not extend some other class). What this all means is that all constructors in the hierarchy are initialized from the top down, that is, most general class to most specific class. So you are guaranteed to have access to all public and proteced (and possibly default) members of any class in the hierarchy.
 
Stephen Adams
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help Michael, I have a further question. If in Class3 I want to call the default constructor of Class2 do I simiply use:
Class3(String fName, String lName){
super();
this.fName = fName;
this.lName = lName;
}
And if it is the default constructor of Class3 it looks like this:
Class3(){
super();
}
Is this right?
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stephen Adams:
Thanks for the help Michael, I have a further question. If in Class3 I want to call the default constructor of Class2 do I simiply use:
Class3(String fName, String lName){
super();
this.fName = fName;
this.lName = lName;
}
And if it is the default constructor of Class3 it looks like this:
Class3(){
super();
}
Is this right?

You can certainly do it like that, but it is not necessary since the compiler will insert a call to super() for every constructor that does not explicitly call some other super constructor. Also, if you don't define a constructor, the compiler will create a default (no args) constructor for you, which by the way, will be exactly what you did in the above code. It won't however, create the default constructor if you define any constructor. So if you need a default constructor and some other constructor, then you must define both.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic