Java Reference
In-Depth Information
Listing 16-25. A Test Class to Test the Manager2 Class
// Manager2Test.java
package com.jdojo.inheritance;
public class Manager2Test {
public static void main(String[] args) {
Manager2 mgr = new Manager2("John Jacobs");
String name = mgr.getName();
System.out.println("Manager name: " + name);
}
}
Manager name: John Jacobs
every class must call the constructor of its superclass from its constructors directly or indirectly. If the superclass
does not have a no-args constructor, you must call any other constructors of the superclass explicitly as we have done
in Listing 16-24.
Tip
I need to discuss a few more rules about using the constructors of the superclass from the subclass. Let's consider
the following definition of classes X and Y , which are in two different packages:
// X.java
package com.jdojo.inheritance.pkg1;
public class X {
// X() has package-level access
X() {
}
}
// Y.java
package com.jdojo.inheritance.pkg2;
import com.jdojo.inheritance.pkg1.X;
public class Y extends X {
public Y() {
}
}
The code for class Y would not compile. It generates a compiler error as follows:
Error(7): X() is not public in com.jdojo.inheritance.pkg1.X; cannot be accessed from outside
package
 
Search WWH ::




Custom Search