It is not possible to reduce the visibility of the method of the inherited method from the parent

I got this compiler error:

You cannot reduce the visibility of an 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, the parent class has a func() method, open and overridden by a subclass of TestClass , which is private. Now the compiler throws an error that I cannot reduce the visibility. To say technically, whenever I create a TestClass object, assigning a type to the parent object, since the func() method is overridden, TestClass func () will always be called, why should we care about visibility? What is the reason for this error? Can someone explain me clearly?

+7
source share
3 answers

This is because the subclass has private visibility for the void func() method, but the superclass has public visibility.

If your code was allowed to compile, it will explode at runtime if you did:

 parent p = new TestClass(); p.func(); // boom - func is public in parent, but TestClass impl is private, so no access would be allowed 

To โ€œfixโ€ this, subclass the func public method:

 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 must start with a capital letter" - ie Parent not Parent

+22
source

From section 8.4.8.3 of the Java language specification :

The access modifier (ยง6.6) of the override or hide method must provide at least the same access as the overridden or hidden method, or a compile-time error. More details:

  • If the overridden or hidden method is publicly available, then the override or hide method must be publicly available; otherwise, a compile-time error occurs.
  • If a protected or hidden method is protected, then the override or hide method must be protected or public; otherwise, a compile-time error occurs.
  • If an overridden or hidden method has default access (package), then the override or hide method should not be closed; otherwise, a compile-time error occurs.

Note that a private method cannot be hidden or overridden in the technical sense of these 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 type of the return type or cast of such a method carry any relation to the method of the private method in the superclass.

In the end, you expect the private method to be called by code inside the same class - if it was called due to an override of the public method, that would be pretty confusing.

+11
source

If you think about it, being unable to do it makes sense.

The reason is because you can pass the child as if it were the parent (for example, you can use the parent type as the reference type for the TestClass instance).

eg.

  parent p = new TestClass(); 

Maybe some code elsewhere that uses parent types and calls this method:

eg.

 public static void aMethod(parent aParent){ p.func(); } 

If you were able to reduce the visibility of the method, then calling aMethod(p) would have to throw some kind of exception at run time - not allowing this to guarantee that it was not required.

+2
source

Source: https://habr.com/ru/post/907030/


All Articles