1 / 14

Inheritance and Polymorphism: Abstract Classes

Inheritance and Polymorphism: Abstract Classes. The “not quite” classes. What if some attributes and behaviors are exactly the same in two (or more) different classes?. Sometimes, instantiable classes may share common behavior for some methods

zed
Download Presentation

Inheritance and Polymorphism: Abstract Classes

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:Abstract Classes The “not quite” classes SE-1020 Dr. Mark L. Hornick

  2. What if some attributes and behaviors are exactly the same in two (or more) different classes? Sometimes, instantiable classes may share common behavior for some methods • For example: Consider classes that representDogs and Cats. Basic behaviors like getName()or getAge() might be identical… • ..while other behaviors may differ, like eat() or speak() In such cases, it would be nice to be able to implement that common behavior in only one place, to avoid unnecessary duplication… • Remember, interfaces only declare methods, but cannot define them SE-1020 Dr. Mark L. Hornick

  3. Why not do this by having both Dog and Cat extend another class like Pet? Maybe, but consider these issues: • Is Pet really a specific type of animal? • What if we’re only dealing with Dogs or Cats? Do we really need a Pet class? • Can we prevent a Pet from being created? Sometimes, a class represents the general aspects of more specific (ie derived) classes, but it doesn’t make sense to actually create objects of the general class. This situation can be handled with a special type of a class that is called an abstract class SE-1020 Dr. Mark L. Hornick

  4. An abstract class is defined with the modifier abstract public abstract class Pet {…} • An abstract class can define any number of attributes and methods, like a regular class • No instances can be created of an abstract class • An abstract class may extend another abstract class SE-1020 Dr. Mark L. Hornick

  5. Example of an abstract class definition public abstractclass Pet { protected String name; // attrdefnprotected int age; // attrdefn… // implementation of shared behavior public void getName() { return name; } // defn public void getAge() { return age; } // defn … } SE-1020 Dr. Mark L. Hornick

  6. Abstract classes can also inherit interface behaviors, but do not have to implement them – delegating the implementation to subclasses public abstractclass Pet implements Animal { protected String name; // attrdefnprotected int age; // attrdefn… // implementation of shared behavior in Pet-derived subclasses: public void getName() { return name; } // defn public void getAge() { return age; } // defn … // Animal methods do not have to be implemented here, although they can be if desired. If not implemented here, they MUST be implemented in regular classes that extend Pet. } SE-1020 Dr. Mark L. Hornick

  7. The Java extendskeyword is used to declare that a class inherits behaviors (and attributes) of another class, including abstract classes public classDog extendsPet implements Mammal{public void shedFur() { // defn of Mammal method } // only behaviors and attributes that are notimplemented in Pet have to be defined here; Pet-defined attributes and behaviors are inherited by Dog // defn of Animal method inherited by Pet public void speak() {System.out.println(“Woof”);} } SE-1020 Dr. Mark L. Hornick

  8. In UML the relationship between class, abstract classes, and interfaces is illustrated as follows: The Generalization connector is used to illustrate that a class extends either a regular class or an abstract classNote the italics used for the abstract Pet class SE-1020 Dr. Mark L. Hornick

  9. Pet myDog, myCat; myCat = newDog(); . . . myCat = newCat(); Polymorphism again allows an interface variable to refer to objects from different classes that implement the interface For example, if Cat and Dog both extend an abstract class called Pet, then the following statements are valid A Dog or a Cat can be used anyplace that expects a Pet • as a method argument: public void feed( Pet p ) • In a collection (e.g. ArrayList<Pet>) SE-1020 Dr. Mark L. Hornick

  10. Inheritance versus Interface Use the Java interface to declare a behavior that must be implemented among related classes • Methods on related classes are used the same way • Example: ArrayList & LinkedList both share common behavior from the List interface • Use extension inheritance to share common implementation • Dog, Cat, Goldfish share implementation of getName() & getAge() SE-1020 Dr. Mark L. Hornick

  11. An abstract method is a method of an abstract class… • …with the keyword abstract, and it ends with a semicolon instead of a method bodypublic abstract void play(); • Instantiable (non-abstract) subclasses of an abstract superclass with abstract methods MUST implement the inherited abstract methods • Similar to overriding, but in this case overriding an unimplemented method SE-1020 Dr. Mark L. Hornick

  12. Abstract Pet class with an abstract play() method: Note that the abstract play() method is shown in italics in the abstract Pet class, but shown in regular font where implemented in the regular classes. CS-1020 Dr. Mark L. Hornick

  13. Why abstract methods? An abstract class may declare abstract methods that MUST be implemented in any (regular) class extending the abstract class • In this way, an abstract method is like an interface method, which means that the declared behavior MUST be implemented at some point in the inheritance heirarchy. SE-1020 Dr. Mark L. Hornick

  14. Rules for abstract methods… abstractmethods may not be declared private or static • Since private and static methods cannot be overridden in subclasses SE-1020 Dr. Mark L. Hornick

More Related