[18 marks] (Inheritance) Design a class named Person and its two subclasses name
ID: 3736373 • Letter: #
Question
[18 marks] (Inheritance) Design a class named Person and its two subclasses named Student and Employee. A person has a name, an address (a String type), a phone number and an email address. A student has a year level number (1,2,3, or 4). An employee has a office and salary. Override the toString method in all the three classes to display the class name. Create appropriate constructor(s) for each class. But do not need to create any 3. accessor or mutator for any of the classes. Outside any of the classes above, define a method with the following header: public static void displayObject(Person object) This method calls the toString0 method of the Person object. Implement the classes and the method above. Write a test program that creates a Person object, a Student object, and an Employee object, and then call the displayObject metho three times to pass the three objects above to the method, respectively, to display the cl name of each object public class TesterExplanation / Answer
class Person // base class
{
private String name;
private String address;
private String phoneNumber;
private String email;
public Person()//default constructor
{
name = "";
address = "";
phoneNumber = "";
email="";
}
public Person(String name,String address,String phoneNumber,String email)//argument constructor
{
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
this.email = email;
}
public String toString()
{
return " class name is: " + getClass().getName(); // display class name
}
}
class Employee extends Person // derived class
{
private String office;
private double salary;
public Employee()// default constructor
{
office = "";
salary = 0.0;
}
public Employee(String name,String address,String phoneNumber,String email,String office,double salary)
{
super(name,address,phoneNumber,email);
this.office = office;
this.salary = salary;
}
public String toString()
{
return " class name is: " + getClass().getName();// display class name
}
}
class Student extends Person //derived class
{
private int yearLevelNumber;
public Student()
{
yearLevelNumber = 0;
}
public Student(String name,String address,String phoneNumber,String email,int yearLevelNumber)
{
super(name,address,phoneNumber,email);
this.yearLevelNumber = yearLevelNumber;
}
public String toString()
{
return " class name is: " + getClass().getName();//display class name
}
}
class Tester
{
public static void displayObject(Person object)
{
System.out.println(object.toString());// calling toString() method
}
public static void main (String[] args)
{
//objects of classes
Person p = new Person("John Smith","3445- downStreet,NY","0988-878787","john@lkmail.com");
Student s = new Student("Candy Johnson","876- NJ","4464-67768","xyz@mail.com",4);
Employee e = new Employee("Andrew Edison","6575- LA","76768-678686","andrew@gtmail.com","Accounts",5660.00);
//call displayObject with object as argument
displayObject(p);
displayObject(s);
displayObject(e);
}
}
Output:
class name is: Person
class name is: Student
class name is: Employee
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.