Java Reference
In-Depth Information
The error states that the no-args constructor in class X has a package-level access. Therefore, it cannot be
accessed from class Y , which is in a different package. You received this error because the compiler will modify
class Y definition as follows:
// Compiler modified version of class Y
// Y.java
package com.jdojo.inheritance.pkg2;
import com.jdojo.inheritance.pkg1.X;
public class Y extends X {
public Y() {
super(); // Injected by the compiler to call X() constructor
}
}
The no-args constructor of class X has a package-level access. Therefore, it can only be accessed from
com.jdojo.inheritance.pkg1 package. How do you fix class Y ? It is tricky to suggest a solution in such a case.
The solution depends on the design that is used behind the creation of class X and class Y . However, for the class Y to
compile, you must create a constructor for the class X , which has a public or protected access, so it can be accessed
from class Y .
Here is another rule for using constructors along with inheritance. The superclass constructor must be called
explicitly or implicitly from inside the constructor of a class using the super keyword. However, the access to a
superclass constructor from a class is controlled by the access level of the constructor of the superclass. Sometimes,
consequences of the access level of the constructors of a class could be that it cannot be accessed at all. Consider the
following definition of the class called NoSubclassingAllowed :
public class NoSubclassingAllowed {
private NoSubclassingAllowed() {
}
// Other code goes here
}
NoSubclassingAllowed has explicitly declared a private constructor. A private constructor cannot be accessed
from anywhere including subclasses. For a subclass to exist, it must be able to call at least one of the constructors of
its superclass. This concludes that the NoSubclassingAllowed class cannot be inherited by any other classes. This
is one of the ways to disable inheritance for a class. The following code will not compile, which tries to subclass the
NoSubclassingAllowed class, which has no accessible constructors:
// Won't compile.
public class LetUsTryInVain extends NoSubclassingAllowed {
}
One thing you may notice is that no one can create an object of the NoSubclassingAllowed class because its
constructor is not accessible from outside. Classes like this one provide methods that create its object and return it to
the caller. This is also a way to control and encapsulate object creation of a class.
Recall from Chapter 6 that you can call a constructor of a class from another constructor of the same class using
the this keyword and this call must be the first statement in the constructor's body. When you look at the rule to call
another constructor of the same class and constructor of the superclass, you would find that both state that the call
must be the first statement inside the body of the constructor. The result of these two rules is that from one constructor
either you can use this() to call another constructer of the same class or super() to call a constructor of the superclass,
but not both. This rule also ensures that the constructor of the superclass is always called once and only once.
 
Search WWH ::




Custom Search