Raghav
Raghav

Reputation: 199

Why this code doesn't call the subclass? Inheritance in Java

public class Parent {
    public  void printParent()
    {
        System.out.println("I am the Parent");
        System.out.println("----this is ::---" + this);
         this.printChild();
    }
    private void printChild()
    {
        System.out.println("This is my child");
    }
}

public class Child extends Parent {
    private void printChild()
    {
        System.out.println("I am the child");
    }
}

public class RelationshipTester {
    @Test
    public  void testRelation()
    {
        Child parent = new Child();
        parent.printParent();
    }
}

This is the output :-

I am the Parent

----this is ::---datastructures.lists.inheritance.Child@1a692dec

This is my child

The object is of the type Child , yet it doesn't call the child method and the parent one. I have given this.printChild();

Upvotes: 4

Views: 266

Answers (6)

Rohit Goyal
Rohit Goyal

Reputation: 550

Since the scope of the method is private it is not visible to other class.

Upvotes: 0

Pulipati Prasadarao
Pulipati Prasadarao

Reputation: 115

In this program we have to remember 2 points:

  1. If we create an object for a subclass (here child) then memory also created for the super class. That means if the method that we are calling that is not found in the child class then Java Virtual Machine goes to parent class and checks for the method that we are calling and if the method is found it will execute. If the method is found in child class itself it will not go to the parent class.

  2. private, static and final methods can't be overridden.

Upvotes: 0

wooden
wooden

Reputation: 1

public class Child extends Parent {
    protected void printChild(){
        System.out.println("I am the child");
    }
}

use protected, not private

Upvotes: 0

Stephen C
Stephen C

Reputation: 719641

In the Parent class, you have declared printChild as private ... and called it. A private method cannot be overridden. Your printChild in the Child class is not known to the Parent class.

If you were to change the private modifier to public, then you would have an override, and your example should output what you are expecting.


Why won't Java let you override a private method? Well, basically, if you could do it then there would be no way to write a class with an abstraction boundary that a child class could not break. That would (IMO) be a major language design short-coming.

Why doesn't Java report an error or warning? Well there is no error because this is legal Java according to the JLS. As for a warning ... if you compile Parent in isolation there is no problem, because the code as written is declaring and using a private method. If you compile Child in isolation, the compiler can't see the private method in the Parent class. (Indeed, it may not even exist in the version of the .class file for Parent that you are compiling against.) Only if you compiled Parent and Child at the same time might the compiler spot something a bit odd.

Upvotes: 5

Lucas Ross
Lucas Ross

Reputation: 1099

private methods are not inherited. When you call printParent, you're calling a method on Parent and when that method refers to this, it's referring to an instance of that class (Parent), which has its own printChild method. Making Parent#printChild a protected method should give the expected result.

Upvotes: 0

Manas
Manas

Reputation: 5

The keyword 'this' point to the current class instance and here is the private void printChild(). And then you have created an object of class Child at RelationshipTester class. The scope of the two function is private which means to is bounded to that class only. Thus, it won't overwrite the subclass and would execute the Base class's method.

Upvotes: 0

Related Questions