Change the access modifier of an overridden method

2019-02-08 04:48发布

Is there a reason one can change the access modifier of an overridden method? For instance,

abstract class Foo{
    void start(){...}
}

And then change the package-private access modifier to public,

final class Bar extends Foo{
    @Override
    public void start(){...}
}

I'm just asking this question out of curiosity.

5条回答
我只想做你的唯一
2楼-- · 2019-02-08 05:07

The explaination is this:-

It's a fundamental principle in OOP: the child class is a fully-fledged instance of the >parent class, and must therefore present at least the same interface as the parent class. >Making protected/public things less visible would violate this idea; you could make child >classes unusable as instances of the parent class.

 class Person{
 public void display(){
  //some operation
 }
 }

class Employee extends Person{
private void display(){
   //some operation
 }


 Person p=new Employee();

Here p is the object reference with type Person(super class),when we are calling >p.display() as the access modifier is more restrictive the object reference p cannot access child object of type Employee

查看更多
一纸荒年 Trace。
3楼-- · 2019-02-08 05:09

Extending a class means the subclass should at least offer the same functionality to the other classes.

If he extends that, then it is not a problem.

Extending could be be either adding new methods or by offering existing methods to more classes like making a package-access method public.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-02-08 05:12

There is only one, you might want the override to be visible by more classes, since no modifier is default, public broadens that.

查看更多
5楼-- · 2019-02-08 05:22

Edit: OK, I changed my answer to fix the problem.

If that couldn't be done, then there would be some cases where a class wouldn't be able to implement an iterface and extend a class because they have the same method with different access modifiers.

public Interface A {
  public void method();
}

public abstract classs B {
  protected void method();
}

public class AB extends B implements A {
  /*
   * This would't be possible if the access modifier coulnd't be changed
   * to less restrictive
  */
  public void method();
}
查看更多
Rolldiameter
6楼-- · 2019-02-08 05:24

Java doesn't let you make the access modifier more restrictive, because that would violate the rule that a subclass instance should be useable in place of a superclass instance. But when it comes to making the access less restrictive... well, perhaps the superclass was written by a different person, and they didn't anticipate the way you want to use their class.

The programs people write and the situations which arise when programming are so varied, that it's better for language designers not to "second-guess" what programmers might want to do with their language. If there is no good reason why a programmer should not be able to make access specifiers less restrictive in a subclass (for example), then it's better to leave that decision to the programmer. They know the specifics of their individual situation, but the language designer does not. So I think this was a good call by the designers of Java.

查看更多
登录 后发表回答