Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot reduce visibility of method inherited method from parent [duplicate]

Tags:

java

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 ?

like image 964
Lolly Avatar asked Jan 27 '12 14:01

Lolly


People also ask

Can we reduce the visibility of the inherited method?

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.

Can we change the visibility of method while overriding If yes then what is condition in it?

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).

Can we increase the visibility of the overridden method?

Asking for help, clarification, or responding to other answers. Making statements based on opinion; back them up with references or personal experience.

Can you override inherited methods?

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.


2 Answers

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

like image 113
Bohemian Avatar answered Sep 28 '22 09:09

Bohemian


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.

like image 32
Jon Skeet Avatar answered Sep 28 '22 09:09

Jon Skeet