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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling constructors of inherited classes" Watch "Calling constructors of inherited classes" New topic
Author

Calling constructors of inherited classes

Stephen Adams
Greenhorn

Joined: Mar 18, 2002
Posts: 22
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

Joined: Jan 30, 2002
Posts: 3451
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.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Stephen Adams
Greenhorn

Joined: Mar 18, 2002
Posts: 22
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

Joined: Jan 30, 2002
Posts: 3451
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling constructors of inherited classes
 
Similar Threads
Class Communications
How to invoke a grand-parent method?
class inheritance
Accessing component from main class.... Help!
call to JTextArea.getText() is not getting current text