Presentation is loading. Please wait.

Presentation is loading. Please wait.

SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Interfaces.

Similar presentations


Presentation on theme: "SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Interfaces."— Presentation transcript:

1 SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Interfaces

2 SE-1020 Dr. Mark L. Hornick 2 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

3 SE-1020 Dr. Mark L. Hornick 3 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

4 SE-1020 Dr. Mark L. Hornick 4 The Java interface is used to declare a behavior (only method headers) that will be defined in classes implementing that interface public interface Animal { public static final int FURRY=1; // attr defn public static final int SCALY=2; // attr defn … public void eat(); // method decl public void speak(); // method decl … } Remember: Interfaces are not classes: They cannot be instantiated They declare public, non-static methods, but do not define them They can only define public static attributes (constants)

5 SE-1020 Dr. Mark L. Hornick 5 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

6 In UML the relationship between an interface and the classes that implement it is illustrated as follows: SE-1020 Dr. Mark L. Hornick 6 Realization is 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

7 SE-1020 Dr. Mark L. Hornick 7 A regular class can extend only one class, but can implement more than one interface public class Dog extends Pet implements Mammal, Animal { public void shedFur() { // defn of Mammal method System.out.println(“Woof. I’m shedding.”); } public void eat() { // defn of Animal method System.out.println(“Yum! A bone!”); } public void speak() { // defn of Animal method System.out.println(“Bark!”); } }

8 SE-1020 Dr. Mark L. Hornick 8 Polymorphism allows an interface variable to refer to objects from different classes that implement the interface For example, if Cat and Dog both implement an interface called Animal, then the following statements are valid Animal myDog, myCat; myCat = new Dog();... myCat = new Cat(); 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 )

9 SE-1020 Dr. Mark L. Hornick 9 Recall: The instanceof operator can be used to dermine the actual class of an object. int dogCount=0, catCount=0; for (Animal a: kennel ) {//kennel is ArrayList if ( a instanceof Dog ) { dogCount++; } else if ( a instanceof Cat ) { catCount++; }

10 SE-1020 Dr. Mark L. Hornick 10 The ArrayList and LinkedList classes both implement the List interface // We can use List to declare a variable that can // refer to either an ArrayList or LinkedList List room = new ArrayList (); List cage = new LinkedList (); for( Animal a: room) { // iterate the ArrayList a.eat(); } for( Animal a: cage) { // iterate the LinkedList a.eat(); } The foreach loop can be used on any collection that implements the List interface

11 SE-1020 Dr. Mark L. Hornick 11 The Collections class can perform various operations to a collection that implements the List interface // We can use List to declare a variable that can // refer to either an ArrayList or LinkedList List room = new ArrayList (); List cage = new LinkedList (); Collections.shuffle(room); // reorders the List using Collections.shuffle(cage); // the static shuffle method See the Sun Javadoc for the Collections class

12 SE-1020 Dr. Mark L. Hornick 12 The Comparable interface defines a single method that is used to compare two objects public interface Comparable { /** 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 int compareTo(T otherObject); } See the Sun Javadoc for the Comparable interface

13 SE-1020 Dr. Mark L. Hornick 13 The Collections class can sort a collection that implements the List interface, provided that the elements of the List implement the Comparable interface // We can use List to declare a variable that can // refer to either an ArrayList or LinkedList List room = new ArrayList (); List cage = new LinkedList (); Collections.sort(room); // reorders the List using Collections.sort(cage); // the static sort method Note: Animal must extend the Comparable interface: public interface Animal extends Comparable


Download ppt "SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Interfaces."

Similar presentations


Ads by Google