Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i know about variable shadowing but i don't think below is case of variable shadowing  

<pre>
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();
        e.show(); // prints null
	}
}



class Person {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class Employee extends Person{
    void show (){
        System.out.println(getName());
    }
}


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.
Posted
Updated 2-Sep-19 5:07am
Comments
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

Quote:
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,
Java
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.
Java
Person p = new Employee();
Employee e = (Employee)p;
e.show();
This is similar to having this,
Java
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,
Java
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.

Java
Person p = new Person();
p.setName("Java");
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)[^]
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900