Answer:
class Person{
String name;
String address;
int phoneNumber;
public Person(){}
public Person(String name, String address, int phoneNumber){
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
}
public String getName(){
return name;
}
public String getAddress(){
return address;
}
public int getPhoneNumber(){
return phoneNumber;
}
public void setName(String name){
this.name = name;
}
public void setAddress(String address){
this.address = address;
}
public void setPhoneNumber(int phoneNumber){
this.phoneNumber = phoneNumber;
}
}
class Customer extends Person{
int customerNumber;
boolean addToMailingList;
Customer(String name, String address, int phoneNumber, int customerNumber, boolean addToMailingList){
super(name, address, phoneNumber);
this.customerNumber = customerNumber;
this.addToMailingList = addToMailingList;
}
@Override
public String toString(){
return "\\\\The customer name is: " + name + ".\t The customer address is: " + address + ".\t The phone number is also: " + phoneNumber + ".\\The customer number is: " + customerNumber + ".\tThe option to add to mailing list is: " + addToMailingList;
}
}
public class Main{
public static void main(String[] args){
Customer customer1 = new Customer("John Doe", "Silicon Valley", 12345, 1100, true);
Customer customer2 = new Customer("James Smith", "Epic Towers", 56788, 1100, false);
Customer customer3 = new Customer("Grace Hooper", "Naval base", 11223, 1110, true);
System.out.println(customer1);
System.out.println(customer2);
System.out.println(customer3);
}
}
Step-by-step explanation:
First, the Person class was defined with the name, address and telephone number field. The Person class also contain constructor for the class.
Next, a customer class was created that extended the Person class. The customer class also has new field of customerNumber and 'addToMailingList'. the toString method was also override in the customer class so as to create a readable output.
The Main class was then created where three instance of the Customer class was created and printed.