









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Walk()
Walk()
public abstract class LivingThing { private String name; public LivingThing(String name){ this.name = name; } public void breath(){ System.out.println("Living Thing breathing..."); } public void eat(){ System.out.println("Living Thing eating..."); } /
public class Main { public static void main(String[] args) { // Create Human object instance and assign it to Human type. Human human1 = new Human(“Ali"); human1.walk(); // Assign Human object instance to LivingThing type. LivingThing livingthing1 = human1; livingthing1.walk(); // Create a Monkey object instance and assign it to LivingThing type. LivingThing livingthing2 = new Monkey(“Sandy"); livingthing2.walk();
// Display data from human1 and livingthing1. // Observe that they refer to the same object instance. System.out.println("human1.getName() = " + human1.getName()); System.out.println("livingthing1.getName() = " + livingthing1.getName()); // Check of object instance that is referred by x and y is the same object instance. boolean b1 = (human1 == livingthing1); System.out.println("Do human1 and livingthing1 point to the same object instance? "
Create another class “Main” which contains main method. Create an object instance of Person class named as Person1. Call computeTotalwealth method. Assign Person object instance to PersonInterface type named as PersonInterface1. Call computeTotalwealth method. Display data from person1 and personinterface1. Check that object instance that is referred by person1 and personinterface type is the same object instance. Create an object instance of Person class and assign it to the interface type directly named as PersonInterface2. Call computeTotalwealth method.
public class Person implements PersonInterface { int cashSaving; int retirementFund; String firstName; String lastName; // Constructor with arguments Person(int cashSaving, int retirementFund, String firstName, String lastName){ this.cashSaving = cashSaving; this.retirementFund = retirementFund; this.firstName = firstName; this.lastName = lastName; } // Compute person's total wealth public int computeTotalWealth(){ System.out.println((cashSaving + retirementFund));; return (cashSaving + retirementFund); } // Get person's name public String getName(){ return firstName + " " + lastName; } }
public class Main { public static void main(String[] args) { // Create an object instance of Person class. Person person1 = new Person(10000, 20000, "Sang", "Shin"); //You can assign the object instance to // PersonInterface type. PersonInterface personinterface1 = person1; // Display data from person1 and personinterface1. // Observe that they refer to the same object instance. System.out.println("person1.getName() = " + person1.getName() + "," + " person1.computeTotalWealth() = " + person1.computeTotalWealth()); System.out.println("personinterface1.getName() = " + personinterface1.getName()