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.