1

Please have a look at following code.

class TestClass{
private void privMethod()
{
    System.out.println("TestClass Method");
}
public static void main(String... args)
{
     TestClass obj=new SubClass();
     obj.privMethod();
}
}

class SubClass extends TestClass
{
 private void privMethod()
{
    System.out.println("SubClass Method");
}
}

when I compile this code its gets compiled fine. when I run TestClass, the output is: TestClass method The ref. variable obj is of TestClass type but it is referring to a Sub class' object so at run time it should bind with Sub's privMethod(). Can you explain this?

Anurag Shukla
  • 379
  • 1
  • 3
  • 11
  • 1
    Both of your methods print the same thing. What makes you think you can tell which is running by looking at the output? – azurefrog Jul 06 '15 at 19:04
  • sorry , typing error , i have edited it, problem still persisits – Anurag Shukla Jul 06 '15 at 19:07
  • 1
    Essentially, when you have two private methods like this, they're treated as completely different, not overriding each other, but instead exactly as if they had different names. – Louis Wasserman Jul 06 '15 at 19:12
  • The problem is since obj is referring to Object of sublass , when I call obj.privMethod() it should invoke privMethod of subClass at runtime(as instance method are bounded at runtime), so the output should be : SubClass Method. But its not – Anurag Shukla Jul 06 '15 at 19:22

2 Answers2

4

private methods are not overriden by methods in a child class. Only public, protected and package-private ones can be overriden. Therefore polymorphism behavior at runtime is not applied.

If you add the annotation @Override to the declared method in the subclass, you would get a compilation error:

@Override  // error
private void privMethod()
{
    System.out.println("SubClass Method");
}
M A
  • 71,713
  • 13
  • 134
  • 174
  • The main problem is not that of whether privMethod() is over ridden or not But the problem is that of Dynamic binding, we know that instance method are invoked at run-time based on what is the actual object NOT on what is the ref. variable , so it follows that obj.privMethod() should run the privMethod in SubClass not the TestClass . But output us showing the TestClass Method – Anurag Shukla Jul 06 '15 at 19:44
  • @AnuragShukla What you're describing as dynamic binding does not apply to `private` methods. It only applies to methods with the mentioned access modifiers (polymorphism is closely related to method overriding). In this case, the method in the parent class and the method in the subclass are completely unrelated at runtime. – M A Jul 06 '15 at 19:56
  • May be I am missing somthing altogether here, help me where I am going wrong . 1- In any case how can an object of Subclass access the private method of TestClass ? because private method are not inherited by the child class ? – Anurag Shukla Jul 06 '15 at 20:32
  • @AnuragShukla See http://stackoverflow.com/questions/4716040/do-subclasses-inherit-private-fields. `SubClass` cannot access the `private` method in the parent class. But this doesn't mean that an object which is dynamically of type `SubClass` cannot execute it at runtime. – M A Jul 06 '15 at 20:48
  • Yeah, that was pretty helpful. Thanks :) – Anurag Shukla Jul 06 '15 at 20:52
0

private modifier—the field/method is accessible only within its own class.

enter image description here

For more on access specifier.

So although the SubClass extends TestClassbut privMethod() of TestClass is not visible to the SubClass at all. So when you are implementing the privMethod() method in SubClass, it is not overriding the method of TestClass.

To make sure that you are overriding the method of the superclass, you can use @Override Java annotation.If the method does not match a method in the superclass, the compiler will give you the below error

The method privMethod() of type SubClass must override or implement a supertype method

Bacteria
  • 8,406
  • 10
  • 50
  • 67