20
abstract class Base{
      protected abstract void a();
}

class Child extends Base{
      @Override
      public void a(){
          //why is this valid
      }
}

Why is that we can't reduce the visibility but can increase it?

Also I need to implement Template pattern in which the public methods visible can only be of base class.

Example:

abstract class Base{
      public void callA(){
      //do some important stuff
      a();
      }

      protected abstract void a();
}

class Child extends Base{
      @Override
      public void a(){
          //why is this valid
      }
}

Now if java allows to increase visibility then there are two methods visible publicly??

I know interface is one solution but is there some other way out???

0

4 Answers 4

28

Why decreasing visibility is not allowed is already explained in other responses (it would break the contract of the parent class).

But why it is allowed to increase the visibility of a method? First, it would not break any contract, so there is no reason to not allow it. It can be handy sometimes, when it makes sense in the child class for a method to not be protected.

Second, not allowing it could have the side effect of making impossible sometimes to extend a class and implement an interface at the same time:

interface Interface1 {
   public void method();
}

public class Parent {
   protected abstract void method();
}

public class Child extends Parent implements Interface1 {
   @Override
   public void method() {
   }
   //This would be impossible if the visibility of method() in class Parent could not be increased.
}

About your second question, you can do nothing about it. You have to trust that the person who implements the child class doesn't do anything that breaks your implementation. Even if java wouldn't allow to increase visibility, that would still not fix your problem, because a public method with a different name could be created that calls the abstract method:

class Child extends Base{
      @Override
      protected void a(){

      }

      public void a2() {
           a(); //This would have the same problems that allowing to increase the visibility.
      }
}
3
  • 1
    Thanks Pablo. That's what I was looking for, a valid argument of allowing the increase of visibility. Oct 8, 2012 at 12:07
  • Think about the clone method. It´s is protected, yet you should make it protected or public
    – Alex
    Oct 30, 2016 at 16:34
  • There is a third good reason, a subclass can always make another public method available that exposes the protected functionality indirectly. Any attempts to disallow it are therefore pointless, so Java allows you to increase visibility.
    – john16384
    Sep 25, 2017 at 19:01
13

If the base class makes a promise regarding visibility, then the subclass cannot break that promise and still satisfy the Liskov substitution principle. You can't use a subclass in any situation where the promised method is exposed if that promise is broken.

The subclass IS-A base class. If the base class exposes a method, so must the subclass.

There's no way out in Java or C++. I'd guess the same is true in C#.

6
  • I didn't get the answer completely?? So you are saying that I cant restrict the child class from increasing the visibility in any way?? Oct 8, 2012 at 11:34
  • 1
    One question mark per question will do. I think "no" should be easy enough to understand. You cannot make a method less visible in the child than it is in the parent. Let the compiler tell you the answer. It's better than coming here and asking.
    – duffymo
    Oct 8, 2012 at 11:36
  • pardon for question marks. but can you help me with the second example I have written. Oct 8, 2012 at 11:44
  • What's the issue? A child can choose to make a method more visible than its parent does, but it cannot make it less visible. It still satisfies Liskov; it still IS-A parent; you can call the more visible child method a() in any context where the parent method a() is called.
    – duffymo
    Oct 8, 2012 at 11:48
  • yes I understand that principle now. but what can I do to force that base class method is executed, which is my actual problem! see my second example. Oct 8, 2012 at 11:55
5

Why is that we can't reduce the visibility but can increase it?

Suppose that it would be possible to reduce the visibility. Then look at the following code:

class Super {
    public void method() {
        // ...
    }
}

class Sub extends Super {
    @Override
    protected void method() {
        // ...
    }
}

Suppose that you would have another class, in another package, where you use these classes:

Super a = new Sub();

// Should this be allowed or not?
a.method();

To check whether a method call is allowed or not, the compiler looks at the type of the variable you call it on. The type of the variable a is Super. But the actual object that a refers to is a Sub, and there the method is protected, so you would say it should not be allowed to call the method from an unrelated class outside the package. To solve this strange situation, it's made forbidden to make overridden methods less visible.

Note that the other way around (making a method more visible) doesn't lead to the same problem.

2
  • I got it now thanks. But can you help me with the question of template method? where I need to do some important stuff in base class and if two methods are publicly available then I cant keep control in base class? Oct 8, 2012 at 11:43
  • Semantically, there would be no problem allowing a child class to hide parent members. Code which receives an iterator may call remove on it since the caller might only pass iterators that support that method; nothing is gained, however, by allowing code which receives a strongly-typed reference to an immutable collection's iterator implementation to call remove on it, since that operation couldn't possibly work. The danger with allowing a private override is that the child class might have been intending to create a private function rather than override a public one.
    – supercat
    Feb 23, 2015 at 19:47
3

Since Java allows Super class reference to point to sub class object.. So, restriction should not be increased from compile-time to runtime..

Lets see this through an example: -

public class B {
    public void meth() {

    }
}

class A extends B {
    private void meth() {  // Decrease visibility.

    }
}

Now, you create an object of class A and assign it the reference of class B.. Lets see how: -

B obj = new A();  // Perfectly valid.

obj.meth();  // Compiler only checks the reference class..
             // Since meth() method is public in class B, Compiler allows this..
             // But at runtime JVM - Crashes..

Now, since compiler only checks the type of the reference variable, and check the visibility of methods in that class (class B), and it doesn't check what kind of object does the reference obj refers to.. So, it is not worried about that.. It is left to JVM at runtime to resolve the appropriate method..

But at runtime, JVM will actually try to invoke the meth method of class A as object is of class A.. But, now what happens... BooooOOMM ---> JVM Crashes.. because meth method is private in class A...

That's why visibility is not allowed to be decreased..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.