3

The following program fails:

abstract class A {
  protected void method() {}
}

class B extends A {
  private void method() {}
}

public class main{
     public static void main(String []args) {}
}

with:

main.java:6: error: method() in B cannot override method() in A
  private void method() {}
               ^
  attempting to assign weaker access privileges; was protected
1 error

Setting the derived method to protected/private works.

Question: What is the reason Java won't let you further restrict access in subclasses? I contrast this to C++ which has the exact opposite rule.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
eplictical
  • 583
  • 1
  • 6
  • 16
  • See [Virtual Function in JAVA](http://www.shivasoft.in/blog/java/virtual-function-in-java/) – PM 77-1 May 09 '14 at 20:25
  • 2
    @Vishrant is not a duplicate. That question ask about how to accomplish that behavior in Java. This question asks **why** Java has this behavior. – Luiggi Mendoza May 09 '14 at 20:27
  • @LuiggiMendoza: This one seems like an exact duplicate -http://stackoverflow.com/q/11484667/738746. – Bhesh Gurung May 09 '14 at 20:29
  • 1
    @BheshGurung looks like an incomplete explanation. – Luiggi Mendoza May 09 '14 at 20:32
  • This is the Thumb rule of polymorphism, if user x override the method of user y and made it private then user x will loos control on his/her own method then who is going to use x method next time and what is the use of it. – Hello World May 09 '14 at 20:34
  • @P.K普 what part of *C++ has the exact opposite rule* you didn't read on the question? – Luiggi Mendoza May 09 '14 at 20:35
  • @LuiggiMendoza:I am not talking about c++ the question main concern is about java. – Hello World May 09 '14 at 20:37
  • @LuiggiMendoza: Agreed. It's good on mentioning the LSP, but nothing about all methods being virtual. – Bhesh Gurung May 09 '14 at 20:39
  • 1
    @P.K普 the question is why Java has this behavior unlike C++. And the explanation is that methods in Java are `virtual`. In C++, you have to declare your method as `virtual` manually to allow polymorphism. And that's the explanation. – Luiggi Mendoza May 09 '14 at 20:39
  • @BheshGurung if you don't mark the method as `virtual`, it won't be overridden at all, both methods would have almost the same signature but behave different. – Luiggi Mendoza May 09 '14 at 20:40
  • 1
    @LuiggiMendoza: I am not that well familiar with C++. But I see what you are saying. I have also edited the title, IMHO, it represents the question better now. – Bhesh Gurung May 09 '14 at 20:42
  • @BheshGurung I recommend you to practice some C++ to have a better understanding of polymorphism in other languages. – Luiggi Mendoza May 09 '14 at 20:42

2 Answers2

7

In Java, all methods are virtual by default (except for private and static methods). So, when overriding a method in Java, it must behave with the definition in the class of the object reference. By this rule in Java, you cannot narrow the visibility of an overridden method, just maintain the same visibility or widening it.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
5

All Java methods are virtual. So suppose you get an A that is actually an instance of B. You don't know this. You call A.method(), but because method is virtual, it tries to call B.method(). But B.method is private, so now what?

Edit: apparently Java methods are only virtual by default these days, as Luiggi Mendoza points out in his answer.

Further edit: I guess the scenario I've described is one where A.method is public, where you had it as protected. But you get the idea.

adv12
  • 8,443
  • 2
  • 24
  • 48