0
public class Parent {
    String name;
}

public class Child extends Parent {
    String name;
}

public static void main(String[] args) {
    Parent p = new Parent();
    Child c = new Child();
    c.name = "jai";
    p.name = "anil";
    Parent pc = c;
    pc.name = "dev";
    System.out.println(pc.name); //Output is dev
    System.out.println(p.name); //Output is anil
    System.out.println(c.name); //Output is jai
}

I am not sure why it's storing pc.name separately, as per my understanding, pc.name should result in anil. Please help in understanding what I am missing!

  • 1
    You cannot override fields, `name` in `Child` is something entirely different compared to `name` in `Parent`. Each `Child` basically has two names right now. And having a class `Parent` and `Child` does not make much sense, both are `Person` and the fact that one is a child / parent is dependant on wether or not they have a field `Person parent` that is either `null` or another actual `Person`. – luk2302 Nov 05 '20 at 12:05

3 Answers3

0

You missunderstood the concept of Inheritance. If child extends Parent that means it Inherits attributes and methods. Therefor you dont have declare name in child once again, because it already gets that attribute from the parent class. Now you create 3 Objects and make the names c, p and pc. But it does not matter which names they have. These 3 Objects have nothing to do with each other. If you want to know more about inheritance look here https://www.w3schools.com/java/java_inheritance.asp

Tobias
  • 383
  • 2
  • 12
0

In a class hierarchy, when a variable in a subclass has the same name as its superclass, then the object of the subclass will always refer to the version of the variable defined by the subclass. The version of the variable defined by the superclass will be hidden. After changing the variable name in Child class, as in the following code

class Parent {
    String name;
}

public class child extends Parent {
     String addr;
public static void main(String[] args) {
    Parent p = new Parent();
    child c = new child();
    c.name = "jai";
    c.addr = "LA";
    p.name = "anil";
    Parent pc = c;
    pc.name = "dev";
    System.out.println(pc.name); //Output is dev
    System.out.println(p.name); //Output is anil
    System.out.println(c.name); //Output is jai
}
}

gives the desired result i.e. pc = c assigns same names to pc, and c, pc.name = "dev"; overwrites the c as well.

dev
anil
dev
0

You define a variable with the same name as a variable in a parent class when you want to hide a variable. This creates two copies of the variable within an instance of the child class: one instance defined for the parent reference and another defined for child reference.

So, if you are referencing the variable from within parent class, the variable defined in parent class is used. Alternatively, if you are referencing the variable from within child class, the variable defined in the child class is used.

Note: if you would like to reference the variable defined in the parent, you can explicitly call it with super.variableName.

Parent p = new Parent();
Child c = new Child();
c.name = "jai"; // child.name
p.name = "anil"; //parent.name
Parent pc = c; // here child.name will be hidden and parent.name be used
pc.name = "dev"; // equal to saying super.name = "dev"; so, c.name <> pc.name

Another example would be:

class A{    
    int age = 10; 
} 
public class B extends A{    
    int age = 15;    
    public static void main(String[] args){        
        B b = new B();        
        b.age = 20;        
        System.out.println(b.age); //prints 20
        System.out.println(  (  (A) b  ).age  ); //prints 10
    } 
}
Aman
  • 1,627
  • 13
  • 19