CSE142 OOP :sLab on Interfaces in Java Date: 5TH April 2012 Implementing a Single Interface: --------------------------------------------Examples # 1:---------------------------------------Creating a Person Interface: public interface PersonInterface { // Compute person's total wealth int computeTotalWealth(); // Get person's name String getName(); } Creating a Person Class that implements the interface: 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; } } Creating a Main class 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() + "," + " personinterface1.computeTotalWealth() = " + personinterface1.computeTotalWealth()); // Check of object instance that is referred by person1 and // personinterface1 is the same object instance. boolean b1 = (person1 == personinterface1); System.out.println("Do person1 and personinterface1 point to the same object instance? " + b1); // Create an object instance of Person class // and assign it to the interface type directly. PersonInterface personinterface2 = new Person(3000, 4000, "Dadu", "Daniel"); System.out.println("personinterface2.getName() = " + personinterface2.getName() + "," + " personinterface2.computeTotalWealth() = " + personinterface2.computeTotalWealth()); } } --------------------------------------------Examples # 2:---------------------------------------Interface RelationInterface // Define an interface with three abstract methods. // Any class that implements this interface has to // implement all three methods. public interface RelationInterface { public boolean isGreater( Object a, Object b); public boolean isLess( Object a, Object b); public boolean isEqual( Object a, Object b); } Class Line that implements the above interface: public class Line implements RelationInterface{ private double x1; private double x2; private double y1; private double y2; // Constructor methoe of the Line class. public Line(double x1,double x2,double y1,double y2){ this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; } // A new method definition of the Line class public double getLength(){ double length = Math.sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)); return length; } // Implement isGreater(..) method defined in the Relation interface public boolean isGreater( Object a, Object b){ double aLen = ((Line)a).getLength(); double bLen = ((Line)b).getLength(); return (aLen > bLen); } // Implement isLess(..) method defined in the Relation interface public boolean isLess( Object a, Object b){ double aLen = ((Line)a).getLength(); double bLen = ((Line)b).getLength(); return (aLen < bLen); } // Implement isEqual(..) method defined in the Relation interface public boolean isEqual( Object a, Object b){ double aLen = ((Line)a).getLength(); double bLen = ((Line)b).getLength(); return (aLen == bLen); } } Main Class public class Main { public static void main(String[] args) { // Create two Line object instances. Line line1 = new Line(1.0, 2.0, 1.0, 2.0); Line line2 = new Line(2.0, 3.0, 2.0, 3.0); boolean b1 = line1.isGreater(line1, line2); System.out.println("line1 is greater than line2: " + b1); boolean b2 = line1.isEqual(line1, line2); System.out.println("line1 is equal with line2: " + b2); // Note that the line3 is object instance of Line type. // Because the Line type is also a type of RelationInterface, // the line3 variable can be declared as RelationInterface type. // This is a very very important concept you need to understand. RelationInterface line3 = new Line(1.0, 5.0, 1.0, 5.0); boolean b3 = line3.isEqual(line1, line3); System.out.println("line1 is equal with line3: " + b3); System.out.println("Length of line1 is " + line1.getLength()); System.out.println("Length of line2 is " + line2.getLength()); // The following line of code will generate a compile error since line3 // is declared as RelationInterface interface type not Line type // and the getLength() method is not one of the methods defined // in the RelationInterface interface. It is commented out for now. // System.out.println("Length of line3 is " + line3.getLength()); } } Implementing Multiple Interfaces: --------------------------------------------Examples # 3:---------------------------------------Interface AnotherInterface : public interface AnotherInterfaceExample { // Measure person's intelligence int measureIntelligence(String name); } Class Person public class Person implements PersonInterface, AnotherInterfaceExample{ 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; } // Implement method of AnotherInterfaceExample public int measureIntelligence(String name){ if (name.startsWith("smart")){ return 100; } else{ return 50; } } } Class Main 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() + "," + " perosn1.measureIntelligence() = " + person1.measureIntelligence(person1.getName())); System.out.println("personinterface1.getName() = " + personinterface1.getName() + "," + " personinterface1.computeTotalWealth() = " + personinterface1.computeTotalWealth()); // Compile error is expected on the following line of code. // personinterface1.measureIntelligence(personinterface1.getName()); // You can assign the object instance to // AnotherInterfaceExample type. AnotherInterfaceExample anotherinterfaceexample1 = person1; // Check of object instance that is referred by personinterface1 and // anotherinterfaceexample1 is the same object instance. boolean b1 = (personinterface1 == anotherinterfaceexample1); System.out.println("Do personinterface1 and anotherinterfaceexample1 point to the same object instance? " + b1); } } ----------------------------- The End---------------------------------------