Reputation: 4732
I have question regarding polymorphism about its assignment statement, for Example this is The Super class
public class Person {
private String firstName;
private String middleInitial;
private String lastName;
private int age;
public Person(String firstName,String middleInitial , String lastName , int age){
setFirstName(firstName);
setMiddleInitial(middleInitial);
setLastName(lastName);
setAge(age);
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getFirstName(){
return firstName;
}
public void setMiddleInitial(String middleInitial){
this.middleInitial = middleInitial;
}
public String getMiddleInitial(){
return middleInitial;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public String getLastName(){
return lastName;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public String toString(){
return String.format("First Name: "+getFirstName()+"\nMiddle Initial: "+getMiddleInitial()+
"\nLast Name: "+getLastName()+"\nAge: "+getAge());
}
}
And this is The Derived Class
public class Employee extends Person{
private Contact contact;
public Employee(String firstName,String middleInitial , String lastName , int age, Contact contact){
super(firstName,middleInitial,lastName,age);
this.contact = contact;
}
public String toString(){
return String.format(super.toString()+contact.toString());
}
}
Now my question is what's the Difference Between these assignment statements ?? and what are the difference ? I know Employee is a Person but I want to know what's the difference between these two:
Person employee1 = new Employee();
Employee employee2 = new Employee();
and This
Employee employeeKyle = new Employee();
Person employeeKyel2 = employeeKyle;
I am kinda having a hard time about these .
Upvotes: 0
Views: 1682
Reputation: 22822
In your second example, you only have one object, with two pointers to it. If you modify employeeKyle
, then employeeKyle2
will be modified as well (they are the same object). The fact that you refers to employeeKyle2
as a Person and not an Employee
is only helpful for the methods you're allowed to use on that reference. Under the hood, it's still pointing to the same Employee
object, so if you later cast employeeKyle2
as an Employee
, it will work and not throw a cast exception.
In the first example, you create two different objects, that's the only difference. They are two Employee
objects under the hood, but you can only use Person
methods on employee2
.
Upvotes: 1
Reputation: 160191
The first one creates an Employee
that is referenced as a Person
, and an Employee
referenced as an Employee
. employee1
has only Person
methods, while employee2
has both Person
and Employee
methods.
The second one creates an Employee
and then creates a Person
reference to the same Employee
. Only methods defined in the Person
class will be available to the employeeKyel2
instance, while employeeKyle
will have all the Employee
methods.
Upvotes: 1
Reputation: 1117
In the first one you have 2 OBJECTS, both are of the type Employee, and you have 2 different references to them, one being a Person reference and the other being an Employee reference.
In the second case, you have 1 OBJECT with 2 REFERENCES pointing to it, being one a Person and another an Employee.
The object is always of the type you've instantiated it with the new keyword (in this case, the object is always an employee). You can, however, have different types of references pointing to the actual object (the references can be either of the type of the object or of one of its superclasses).
Upvotes: 2