In: Computer Science
language is Java
Design a super class Person with the fields for holding a person's name, address, and phone number. Code at least two overloaded constructors and the appropriate mutator and accessor methods. Next, design a subclass Customer that inherits from the Person class. In addition to inherit all data and methods from its super class, the Customer class should have a field for a customer ID and a boolean field indicating whether or not the customer whishes to be on a mailing list. Code appropriate constructors, mutator and accessor methods for added fields in Customer class.
Then code a driver class InheritanceApp to test the classes by creating at least one object for Person class and at least one object for Customer class to display all field information using overridden toString(), respectively. You my do hard- coded data in test.
Must code Person and Customer classes as your operation classes with the inheritance relationship and then InheritanceApp as the driver. Run and test your code to meet the requirements.
Must understand the purpose of using inheritance in coding and how well you perform the code-reuseability.
Must create at least one object for each class (Person and Customer).
Must use required/meaningful names for fields, methods and doc each of your source codes.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Make sure you copy below two classes into separate files as mentioned. Do not copy everything to a single file.
//Person.java
public class Person {
// attributes
private String name;
private String address;
private String phoneNumber;
// default constructor
public Person() {
name = "";
address = "";
phoneNumber = "";
}
// constructor taking values for all fields
public Person(String name, String address, String phoneNumber) {
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
}
// getters and setters for all fields
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
// returns a String containing all details of the person
@Override
public String toString() {
return "Name: " + name + ", Address: " + address + ", Phone: "
+ phoneNumber;
}
}
//Customer.java
public class Customer extends Person {
private int customerID;
private boolean onMailingList;
// default constructor
public Customer() {
super(); // invoking super class default constructor
customerID = 0;
onMailingList = false;
}
// constructor taking all values
public Customer(String name, String address, String phoneNumber,
int customerID, boolean onMailingList) {
// passing name, address and phone to super class
super(name, address, phoneNumber);
this.customerID = customerID;
this.onMailingList = onMailingList;
}
// getters and setters
public int getCustomerID() {
return customerID;
}
public void setCustomerID(int customerID) {
this.customerID = customerID;
}
public boolean isOnMailingList() {
return onMailingList;
}
public void setOnMailingList(boolean onMailingList) {
this.onMailingList = onMailingList;
}
@Override
public String toString() {
// appendinga additional attributes values to the string returned by
// super class toString.
return super.toString() + ", CustomerID: " + customerID
+ ", OnMailingList: " + onMailingList;
}
}
//InheritanceApp.java
public class InheritanceApp {
public static void main(String[] args) {
// creating a Person and a Customer object
Person person = new Person("Alice", "12th ABC", "12345678");
Customer customer = new Customer("Bob", "Bob Mansion", "123-777-092",
1129, true);
// displaying both using toString method
System.out.println(person);
System.out.println(customer);
}
}
/*OUTPUT*/
Name: Alice, Address: 12th ABC, Phone: 12345678
Name: Bob, Address: Bob Mansion, Phone: 123-777-092, CustomerID: 1129, OnMailingList: true