11

I am learning java. I have a doubt in inheritance. When a child class extends parent class and parent class has a method which refers to a instance variable declared in parent. But the child class dint override this method and has declared instance variable with same name as the parent. In this case instance variable from child will be referred or parent will be referred. Below is the code snippet

class Parent {
    int a;
    Parent() {
        System.out.println("in Parent");
        a = 10;
    }
    void method() {
        System.out.println(a);
    }
}
class Child extends Parent {
    int a;
    Child() {
        System.out.println("in Child");
        a = 11;
    }
}

public class Test {
    public static void main(String args[]) throws IOException {
        Parent p1 = new Child();
        p1.method();
    }
}

The output I get is

in parent
in child
10

Can someone please make me understand why its referring parent class's instance variable a and not child class's a.

Another doubt is, I understood hiding the method, when there is a static method in parent and child class also has declared a static method with same signature. Here hiding means ? what method is getting hidden ? If its the parent method can you please explain me ?
Thanks in advance.

Java bee
  • 2,522
  • 1
  • 12
  • 25
Lolly
  • 34,250
  • 42
  • 115
  • 150

6 Answers6

20
  1. Java instance variables cannot be overridden in a subclass. Java inheritance doesn't work that way.

  2. In your example, there is no method hiding (or overriding or overloading) going on.

  3. There is hiding of instance variables though. In class child, the declaration of a hides the declaration of a in parent, and all references to a in the child class refer to the child.a not the parent.a.

To illustrate this more plainly, try running this:

public static void main(String args[]) throws IOException {
    child c1 = new child();
    parent p1 = c1;

    System.out.println("p1.a is " + p1.a);
    System.out.println("c1.a is " + c1.a);
    System.out.println("p1 == c1 is " + (p1 == c1));
}

It should output:

    p1.a is 10
    c1.a is 11
    p1 == c1 is true

This demonstrates that there is one object with two distinct fields called a ... and you can get hold of both of their values, if the access permits it.


Finally, you should learn to follow the standard Java identifier conventions. A class name should ALWAYS start with a capital letter.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • good answer! to enforce that same idea that same object has two variables with the same name: `Arrays.stream(Child.class.getFields()) .map(Field::getName) .forEachOrdered(System.out::println);` this, to me, is always, not very intuitive though. – Eugene Apr 26 '18 at 13:13
2

Instance variables are not overriden in sub-class. If you define a variable in your class with the same name as in your super class it's called shadowing of variables inheritance and polymorphism doesn't apply for instance variables. if you define method() in parent and override it in Child class. the below would invoke the Child's method() due to run-time polymorphism printing 11

 parent p1 = new child();
  1. invokes Child constructor
  2. with the super() call invoke's parent's constructor
  3. Print's "in the parent" and initializes Parent's a to 10
  4. print's in child and initializes Childs a to 11

        p1.method();// this invokes Child's method() during run-time
    
NikhilP
  • 1,508
  • 14
  • 23
PermGenError
  • 45,977
  • 8
  • 87
  • 106
1

When you do this

Parent P1=new Child();

what JVM do is

               first  Initialize Parent()

                         ||

               second Initialize Child()

So , first Parent constructor get called and then child's , but the output value will be 11 , because p1 is referring to child's object.

Seyed Ali Roshan
  • 1,476
  • 1
  • 18
  • 37
anshulkatta
  • 2,044
  • 22
  • 30
1

As you are not overriding method() in child class, when the statement,

parent p1 = new child();

is executed, parent version of method() will be executed, and the only a value known to parent class is its own a. hence it will print a=10 (as it is on the stack at that time).

finally, you are just shadowing the variable a from parent class to child class.

Rince Thomas
  • 4,158
  • 5
  • 25
  • 44
prashant khunt
  • 154
  • 3
  • 8
0

The issue is you created a child instance and stored it in a reference of the parent. Hence, when your access the object's property, JVM refers to the parent's variable value.

In case, it would have been a child class's reference variables, you would have received the child class' variable value.

The above is a feature of Java.

Srihari
  • 766
  • 1
  • 6
  • 22
0

As you create the Instance of Parent. so in run time the compiler call the parent object , try the Below code.

public static void main(String args[]) throws IOException {
    child c1 = new child();
    c1.method();
}
Janny
  • 681
  • 1
  • 8
  • 33