I got this compiler error:
You cannot reduce the visibility of a inherited method.
I have the following code
class Parent {
public void func() {
System.out.println("in Parent");
}
}
public class TestClass extends Parent {
public static void main(String args[]) {
parent obj=new TestClass();
obj.addTest();
}
private void func() {
System.out.println("in child");
}
}
Here parent class has func()
method which is public and overridden by the subclass TestClass
which is private. Now the compiler throws the error that I cannot the reduce the visibility. To say technically, whenever I create a object of TestClass
assigning to the type parent object, since the func()
method is overridden, TestClass's func() is going to get called always, then why we should take care of visibility? whats the reason behind this error ? Can someone explain me clearly ?
You cannot reduce the visibility of a inherited method. Here parent class has func() method which is public and overridden by the subclass TestClass which is private.
Change the visibility of the interface and the defined class to default level or declare it in separate files. Only public and abstract modifiers can be applied to interface methods. The class implementing the interface cannot change the visibility of the method (we cannot change it from public to protected).
Asking for help, clarification, or responding to other answers. Making statements based on opinion; back them up with references or personal experience.
A derived class has the ability to redefine, or override, an inherited method, replacing the inherited method by one that is specifically designed for the derived class. The derived class may want to inherit many of the base class's methods because these methods are suited to the behavior of the derived class.
It's because the subclass has visibility of private
for the void func()
method, but the superclass has visibility public
.
If your code was allowed to compile, it would explode at runtime if you did this:
parent p = new TestClass();
p.func(); // boom - func is public in parent, but TestClass's impl is private, so no access would be allowed
To "fix" this, make the subclass's func
method public
:
public class TestClass extends parent {
...
public void func() { // give it public visibility
System.out.println("in child");
}
}
And please use standard naming conventions; in this case "classes should start with a capital letter" - i.e Parent
not parent
From section 8.4.8.3 of the Java Language specification:
The access modifier (§6.6) of an overriding or hiding method must provide at least as much access as the overridden or hidden method, or a compile-time error occurs. In more detail:
- If the overridden or hidden method is public, then the overriding or hiding method must be public; otherwise, a compile-time error occurs.
- If the overridden or hidden method is protected, then the overriding or hiding method must be protected or public; otherwise, a compile-time error occurs.
- If the overridden or hidden method has default (package) access, then the overriding or hiding method must not be private; otherwise, a compile-time error occurs.
Note that a private method cannot be hidden or overridden in the technical sense of those terms. This means that a subclass can declare a method with the same signature as a private method in one of its superclasses, and there is no requirement that the return type or throws clause of such a method bear any relationship to those of the private method in the superclass.
After all, you'd only expect a private
method to be called by code within the same class - if it ended up being called due to overriding a public method, that would be pretty confusing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With