1

When it comes to over - riding , there is only one rule related to access specifiers

"The subclass overridden method cannot have weaker access than super class method"

why is the child class restricted to have stronger access specifier ? what draw back may it invite , I am guessing something to occur on design level.

let us consider the rule didn't exist , in that case 1 : parent class having weaker access say public 2 : child class having stronger access say private

so if any external class tries to access the method , it still can access from child class as it will be inherited in child class and will be available to use with object reference

please clarify.

public class A 
{
    public void methOne(int a)
    {
        // something
    }
}

class B extends A
{
    private void methOne(int a)
    {
        // something else
    }
    }

class C
{
     public static void main(String args[])
     {
          new B().methOne(); 
          // in this special case after removing the specifier rule it will execute the method from parent class.
     }
}

2 Answers2

4

What you have tried to do is invalid, because by extending A all instances of B must be able to act as though they were an A. For example:

A a = new A();
a.methOne(1);

Compared to:

A a = new B();
a.methOne(1);

In both cases all the code knows is that it has an A and that all As have a method called methOne. In the second case that is broken as you have tried to make the method private but A has already said that it has a method by that name available.

It works the other way around because you don't invalidate the contract. If the method was private in A and public in B then you can still use an instance of B as though it was an A, you just don't have access to that method unless you know you are working with a B.

Tim B
  • 40,716
  • 16
  • 83
  • 128
1

When you re declare the method of your parent class in you child class it is method overriding which in simple terms would mean you would want the objects of your child class to call its method instead of the parents method with same name and signature. Now in child class if you have a more restrictive access specifier say by making private as in your case you are effectively telling the JVM not to call the method though it is visible to the child objects which would lead to confusion as if you have exposed only your parent class to outside world then in some of your classes if you could make this method private then JVM shouldnot allow these methods to be called if such child objects are passed .

Q. so if any external class tries to access the method , it still can access from child class as it will be inherited in child class and will be available to use with object reference

This in your above case defeats the purpose of encapsulation by making the method private. By declaring a method private you are actually trying to tell that make my parent class corresponding method not visible to outside world which is not posssible as your parent method is visible to your child class.

Manjunath
  • 1,685
  • 9
  • 9