posted 21 years ago
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