import java.lancs.Person; // // Address class // class Address { // Street private String myStreet; // City private String myCity; // State private String myState; // Constructor public Address (String street, String city, String state) { myStreet = street; myCity = city; myState = state; } // Constructor public Address () { // Initialize of empty strings myStreet = new String (); myCity = new String (); myState = new String (); } public String toString () { return myStreet + " " + myCity + " " + myState; } public String getCity () { return myCity; } public String getStreet () { return myStreet; } public String getState() { return myState; } public void setCity (String city) { myCity = city; } public void setStreet (String street) { myStreet = street; } public void setState(String state) { myState = state; } } // // PersonWithAddressInfo (extend Person with address info) // class PersonWithAddressInfo extends Person { // my address attribute private Address address; // Constructor public PersonWithAddressInfo () { // Construct the super class super (); // Construct the address object address = new Address (); } // Constructor (takes address info as strings) public PersonWithAddressInfo (String street, String city, String state) { // Construct the super class super (); // Construct the address object address = new Address (street, city, state); } // Constructor (takes all pieces of info as string) public PersonWithAddressInfo (String forename, String surname, int age, int gender, String street, String city, String state) { // Call the other constructor this (forename, surname, age, gender, new Address (street, city, state)); } // Constructor (takes most pieces of info as string and the address // as an Address object) public PersonWithAddressInfo (String forename, String surname, int age, int gender, Address addr) { // Construct the super class super (forename, surname, age, gender); // Construct the address object address = addr; } public Address getAddress () { return address; } public String toString () { // Note the call to super.toString() return super.toString () + " " + address.toString (); } } public class Inheritance { // // This method prints a person // public static void print_person (Person person) { System.out.print (person.getForename () + " ") ; System.out.print (person.getSurname () + " (") ; System.out.print (person.getAge () + ", ") ; System.out.println (person.getGenderString () + ")") ; } public static void main (String[] args) { // Create an address Address address = new Address ("White House", "Washington", "D.C"); // Print the address System.out.println (address); // Create an instance of the PersonWithAddressInfo class PersonWithAddressInfo newPerson = new PersonWithAddressInfo ("Irfan", "Pyarali", 25, Person.MALE, "White House", "Washington", "D.C"); // Print the forename System.out.println (newPerson.getForename ()); // Print the surname System.out.println (newPerson.getAge ()); // Get the address Address persons_address = newPerson.getAddress (); // Print the city System.out.println (persons_address.getCity ()); // Another way of printing the city System.out.println (newPerson.getAddress ().getCity ()); // Print person; note that this works because // PersonWithAddressInfo is a Person print_person (newPerson); // Print the object System.out.println (newPerson); } }