1 / 13

Inheritance and Polymorphism: Interfaces

Inheritance and Polymorphism: Interfaces. Sometimes, multiple classes implement similar behavior (via similar methods), even if the details of the behavior are different from class.

miyo
Télécharger la présentation

Inheritance and Polymorphism: Interfaces

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance and Polymorphism:Interfaces 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 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 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 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

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

  6. 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

  7. 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

  8. 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

  9. 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

  10. // 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

  11. // 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

  12. 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

  13. // 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

More Related