140 likes | 286 Views
Inheritance and Polymorphism: Interfaces. Sometimes, multiple classes implement similar behavior (via similar methods), even if the details of the behavior are different from class.
E N D
Inheritance and Polymorphism:Interfaces SE-1020 Dr. Mark L. Hornick
Sometimes, multiple classes implement similar behavior (via similar methods), even if the details of the behavior are different from class. That is, even though the method names and parameters are the same between the classes, the details of the method’s implementation differs from class to class • Example: Every “animal-type” class exhibits a “speak” behavior, although each specific behavior is different SE-1020 Dr. Mark L. Hornick
The common behaviors that must be implemented in various classes can be declared in a special type of “class” called an interface When a class declares that it implements an interface, it contractually guarantees that it actually implements (defines) those behaviors declared by the interface In this way, each class implementing the interface can appear to be the same to a user of that class, because each class contains the exact methods declared in the interface SE-1020 Dr. Mark L. Hornick
The Java interface is used to declare a behavior (only method headers) that will be defined in classes implementing that interface public interfaceAnimal { public static final int FURRY=1; // attrdefnpublic static final int SCALY=2; // attrdefn… public void eat(); // method decl public void speak(); // method decl … } Remember: Interfaces are not classes: • They cannot be instantiated • They declarepublic, non-static methods, but do not define them • They can only definepublic static attributes (constants) SE-1020 Dr. Mark L. Hornick
Implementation of an interface public class Dog implements Animal{ public void eat() {System.out.println(“Yum! A bone!”);} … more code here } public class Cat implements Animal{ public void eat() {System.out.println(“Yum! A mouse!”);} … more code here } Note the distinction here between overriding a base class method and implementing an interface method SE-1020 Dr. Mark L. Hornick
In UML the relationship between an interface and the classes that implement it is illustrated as follows: Realizationis a type of inheritance relationship that implies that a class implements the behavior defined in another class The Realization connector is a dotted line with a triangle (not arrow) pointing at the Interface SE-1020Dr. Mark L. Hornick
A regular class can extendonly one class, but can implementmore than one interface public classDog extendsPet implements Mammal, Animal{public void shedFur() { // defn of Mammal methodSystem.out.println(“Woof. I’m shedding.”);} public void eat() { // defn of Animal methodSystem.out.println(“Yum! A bone!”);} public void speak() { // defn of Animal methodSystem.out.println(“Bark!”);} } SE-1020 Dr. Mark L. Hornick
Animal myDog, myCat; myCat= newDog(); . . . myCat= newCat(); Polymorphism allows an interface variable to refer to objects from different classes that implement the interface For example, if Cat and Dogboth implement an interface called Animal , then the following statements are valid A Dog or a Cat can be used anyplace that expects a Animal • as a method argument: public void feed( Animal p ) • In a collection (e.g. ArrayList<Animal>) SE-1020 Dr. Mark L. Hornick
intdogCount=0, catCount=0; for (Animal a: kennel ) {//kennel is ArrayList<Animal> if ( a instanceofDog ) { dogCount++; } else if ( a instanceofCat ) { catCount++; } } Recall: The instanceof operator can be used to dermine the actual class of an object. SE-1020 Dr. Mark L. Hornick
// We can use List to declare a variable that can // refer to either an ArrayList or LinkedList List<Animal> room = newArrayList<Animal>(); List<Animal> cage = newLinkedList<Animal>(); for( Animal a: room) { // iterate the ArrayList a.eat(); } for( Animal a: cage) { // iterate the LinkedList a.eat(); } The ArrayListandLinkedListclasses both implement the List interface The foreach loop can be used on any collection that implements the List interface SE-1020 Dr. Mark L. Hornick
// We can use List to declare a variable that can // refer to either an ArrayList or LinkedList List<Animal> room = newArrayList<Animal>(); List<Animal> cage = newLinkedList<Animal>(); Collections.shuffle(room); // reorders the List using Collections.shuffle(cage); // the static shuffle method The Collections class can perform various operations to a collection that implements the List interface See the Sun Javadoc for the Collections class SE-1020 Dr. Mark L. Hornick
public interface Comparable<T> { /** Compares this object with the specified other object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified other object. */ public intcompareTo(TotherObject); } The Comparable interface defines a single method that is used to compare two objects See the Sun Javadoc for the Comparable interface SE-1020 Dr. Mark L. Hornick
// We can use List to declare a variable that can // refer to either an ArrayList or LinkedList List<Animal> room = newArrayList<Animal>(); List<Animal> cage = newLinkedList<Animal>(); Collections.sort(room); // reorders the List using Collections.sort(cage); // the static sort method The Collections class can sort a collection that implements the List interface, provided that the elements of the List implement the Comparable interface • Note: Animal must extend the Comparable interface:public interface Animal extends Comparable<Animal> SE-1020 Dr. Mark L. Hornick