Inheritance and Polymorphism Interfaces SE1020 Dr Mark L

  • Slides: 13
Download presentation
Inheritance and Polymorphism: Interfaces SE-1020 Dr. Mark L. Hornick 1

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

Sometimes, multiple classes implement similar behavior (via similar methods), even if the details of

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 l Example: Every “animal-type” class exhibits a “speak” behavior, although each specific behavior is different SE-1020 Dr. Mark L. Hornick 2

The common behaviors that must be implemented in various classes can be declared in

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 3

The Java interface is used to declare a behavior (only method headers) that will

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: l l l They cannot be instantiated They declare public, non-static methods, but do not define them They can only define public static attributes (constants) SE-1020 Dr. Mark L. Hornick 4

Implementation of an interface public class Dog implements Animal{ public void eat() { System.

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 5

In UML the relationship between an interface and the classes that implement it is

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

A regular class can extend only one class, but can implement more than one

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 shed. Fur() { // 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!”); } SE-1020 7 Dr. Mark L. Hornick }

Polymorphism allows an interface variable to refer to objects from different classes that implement

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 my. Dog, my. Cat; my. Cat = new Dog(); . . . my. Cat = 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. Array. List<Animal>) SE-1020 Dr. Mark L. Hornick 8

Recall: The instanceof operator can be used to dermine the actual class of an

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

The Array. List and Linked. List classes both implement the List interface // We

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

The Collections class can perform various operations to a collection that implements the List

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 Array. List or Linked. List<Animal> room = new Array. List<Animal>(); List<Animal> cage = new Linked. List<Animal>(); Collections. shuffle(room); // reorders the List using Collections. shuffle(cage); // the static shuffle method See the Sun Javadoc for the Collections class SE-1020 Dr. Mark L. Hornick 11

The Comparable interface defines a single method that is used to compare two objects

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

The Collections class can sort a collection that implements the List interface, provided that

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 Array. List or Linked. List<Animal> room = new Array. List<Animal>(); List<Animal> cage = new Linked. List<Animal>(); 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<Animal> SE-1020 Dr. Mark L. Hornick 13