Click here to Skip to main content
14,301,484 members
Rate this:
Please Sign up or sign in to vote.
See more:
i know about variable shadowing but i don't think below is case of variable shadowing  

public class Main
	public static void main(String[] args) {
	    Person p = new Person();
        p.setName("Java"); // set name to be =Java 
        Employee e = new Employee();; // prints null

class Person {
    private String name;

    public String getName() {
        return name;

    public void setName(String name) { = name;

class Employee extends Person{
    void show (){

as you guys can see class `Employee` extends `Person` thus it will have all it's fields and method right ? then why when i print `name` in `Employee` it prints null isn't it the same name that i set it to "Java" ? i tried to make the field public but it still didn't worked, i just don't get how the `getName()` method return the value of `name` that i set it on by `setName()` method and it's null !

What I have tried:

i tried many things but couldn't figure it out.
Updated 2-Sep-19 5:07am
CHill60 2-Sep-19 10:43am
You have set e to be a new Employee. That implies a new person also. Try setting it to the Person p that you already have
hiwa doski 2-Sep-19 11:04am
oh yeah that make sense, but how to set employee to the new person i already have as an Employee reference variable can't point to a person object ?
CHill60 3-Sep-19 7:50am
Afzaal's solution 1 is better than anything I could explain :-)
hiwa doski 3-Sep-19 13:42pm
yeah i got it, thank you guys.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

as you guys can see class `Employee` extends `Person` thus it will have all it's fields and method right ?
Only if you will do,
Employee e = (Employee)p;
Then, after a cast, you can see Java being printed. Currently, there is no connection between what p is having and what e contains. Even this will work only and only if you created the instance of an Employee, like this.
Person p = new Employee();
Employee e = (Employee)p;;
This is similar to having this,
String a = "One string";
String b = "Second string";

// And then expecting
System.out.println(b); // to print "One string"
Only to assume this because both are related through type-system, but they are not the same in-memory. You can make them same by linking their references,
String a = "One string";
String b = a; // Linked!

System.out.println(b); // One string
But this gets confusing and a lot cumbersome when dealing with parents and children in Java. Because not only you need to assign the variables but you also need to properly cast them—and handle the ClassCastException. This is what you would get from your code if you would try that cast.

Person p = new Person();
Employee e = new Employee(); 
e = (Employee)p;              // ClassCastException
// OR
Employee e = (Employee) p;    // ClassCastException.
I recommend that you read this to understand how inheritance works in Java.

Inheritance (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)[^]
hiwa doski 2-Sep-19 13:44pm
thank you for your will explained answer, but i still have some confusion about how/when inheriting is done i read the doc you sent me and it kept saying it inherits all field from super class so why the "e" reference variable `name` doesn't have value of Java as it inherits from it, or it does not inherit values ?
Afzaal Ahmad Zeeshan 2-Sep-19 13:56pm
Because they are two different objects. Object inherits, but does not copy the values.

Each child is able to have some wealth by inheritance by parents, but your neighbor will not get the wealth of your father just because he is a son.
hiwa doski 2-Sep-19 14:25pm
aha okay got it, thanks.
CHill60 3-Sep-19 7:51am
Excellent analogy - 5'd
Afzaal Ahmad Zeeshan 3-Sep-19 8:08am
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100