0

With two classes:

Person

/**
 * This class models a person.
 *
 * @author author name
 * @version 1.0.0
 */
public class Person  {

    /* Name of the person */
    private String  name;

    /* Address of the person */
    private String  address;

    /**
     * Constructs a <code>Person</code> object.
     *
     * @param initialName  the name of the person.
     * @param initialAddress  the address of the person.
     */
    public Person (String initialName, String initialAddress) {

        name = initialName;
        address = initialAddress;
    }

    /**
     * Returns the name of this person.
     *
     * @return the name of this person.
     */
    public String getName() {

        return this.name;
    }

    /**
     * Returns the address of this person.
     *
     * @return the address of this person.
     */
    public String getAddress() {

        return this.address;
    }
}

and Employee

/**
 * This class models an Employee.
 *
 * @author author name
 * @version 1.0.0
 */
public class Employee extends Person  {

    /* Salary of the employee */
    private double salary;

    /**
     * Constructs an <code>Employee</code> object.
     *
     * @param initialName  the name of the employee.
     * @param initialAddress  the address of the employee.
     * @param initialSalary  the salary of the employee.
     */
    public Employee (String initialName, String initialAddress,
                     double initialSalary) {

        super(initialName, initialAddress);
        salary = initialSalary;
    }

    /**
     * Returns the salary of this employee.
     *
     * @return the salary of this employee.
     */
    public double getSalary() {

        return this.salary;
    }

    /**
     * Modifies the salary of this employee.
     *
     * @param newSalary  the new salary.
     */
    public void setSalary(double newSalary) {

        salary = newSalary;
    }
}

An employee is-a person, so every Employee object is also a Person object. For this reason, an Employee reference variable can be assigned to a Person reference variable.

Person person = new Employee("Joe Smith", "100 Main Ave", 3000.0);

But can it also be assigned to a Employee reference variable?

Employee employee = new Employee("Joe Smith", "100 Main Ave", 3000.0);

If yes, what is the difference between those two. I would like to grasp the idea of reference and assigning variables so I would be really thankful for clarification.

3 Answers 3

2
(1) Person person = new Employee("Joe Smith", "100 Main Ave", 3000.0);
(2) Employee employee = new Employee("Joe Smith", "100 Main Ave", 3000.0);

Both are correct. (1) creates a Employee and then puts it into a person object. You lose the employee fields/methods, but you can get them with casting like so: Employee employeeFromPerson=(Employee)person. You can reference only the person methods without casting.

(2) basically creates an employee and puts it into a employee object. You can reference Person and Employee methods/fields with this object.

Sign up to request clarification or add additional context in comments.

Comments

1

Both are correct and both have reference of employee objects, but both used in different scenarios.

First is useful for dynamic polymorphism and second is used for simple object assignment.

For example Lets assume this scenario in which two classes Employee and Student extends Person class and both overrides same method. The difference comes in which method is called at runtime.

class Person {
    public String getTag(){
        return "This is Person"
    }
}

class Employee extends Person {
    public String getTag(){
        return "This is Employee"
    }
}


class Student extends Person {
    public String getTag(){
        return "This is Student"
    }
}

Person person1 = new Person();
Person person2 = new Employee();
Person person3 = new Student();
Employee employee = new Employee();
Student student = new Student();

person1.getTag(); //it will return "This is Person"
person2.getTag(); //it will return "This is Employee"
person3.getTag(); //it will return "This is Student"
employee.getTag(); //it will return "This is Employee"
student.getTag(); //it will return "This is Student"

Note that person2 and person3 have reference of child class object, it will call definition of child class method not its own method's definition

Comments

0

It really doesn't matter. You can use either one. If you have class A and subclass of A B and you only want to use A's functionality, then go with A a = new B();.

However, if you use arrays of Person, it is a different story. If you have Person as super class and Manager and Employee as subclasses, then you can put any kind of person into Person[]. However, if you create the array Manager[], you can't put an Employee in there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.