ERROR - Cannot reduce the visibility of inherited method

Error


Cannot reduce the visibility of inherited method 


Error Type 

Compile Time


Sample Code 

public class ParentTest {
    private String element = "Hello";

    public String getString(){
    return element;
    }
}

public class Test extends ParentTest {

private String element = "Hello";

private String getString(){

return element;
}

}

Cause

Java doesn't allow reducing the visibility of inherited method. 


Resolution

Either reduce the visibility of the parent method.  





public class ParentTest {
    private String element = "Hello";
    private String getString(){
     return element;
    }
}

public class Test extends ParentTest {
private String element = "Hello";

private String getString(){
return element;
}
}

or Increase the visibility of the derived method.

public class ParentTest {
    private String element = "Hello";
    public String getString(){
     return element;
    }
}

public class Test extends ParentTest {
public String getString(){
}

private String element = "Hello";

return element;
}