1 / 44

Module 8 “Polymorphism and Inheritance”

Module 8 “Polymorphism and Inheritance”. Outline. Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism Method Overriding The Object Class Abstract Classes and Methods Revisiting Polymorphism. Object-Oriented Programming (OOP).

Download Presentation

Module 8 “Polymorphism and Inheritance”

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. Module 8“Polymorphismand Inheritance”

  2. Outline • Understanding Inheritance • Inheritance Diagrams • Constructors in Derived Classes • Type Compatibility • Polymorphism • Method Overriding • The Object Class • Abstract Classes and Methods • Revisiting Polymorphism

  3. Object-Oriented Programming (OOP) • Three key guiding principles • Encapsulation • Inheritance • Polymorphism

  4. Inheritance • The ability of a class to reuse the status and behavior of another class while adding its own functionality. • The status of a class is the collection of its attributes (or fields). • The behavior of a class is the collection of its methods.

  5. Inheritance Example • Class Person • Attributes: Name • Methods: • Constructors for Person • setName( … ) • getName ( ) • writeOutput ( ) • hasSameName ( )

  6. Inheritance Example • Class Student inherits from Person • Able to use existing Person functionality • It has all of its attributes and methods. • But incorporates additional functionality • Attributes: studentNumber • Methods: • Constructors for Student • reset ( …) • getStudentNumber( ), setStudentNumber( … ) • equals (… )

  7. Inheritance Example • Person is the base (or parent) class • Student is the derived (or child) class • Base classes are more general. • Derived classes are more specific. • Inheritance greatly enhances the ability to reuse existing code. • This simplifies the development time. • And makes design much simpler and cleaner.

  8. Inheritance Example • In Java, a derived class can only have a single base class. • Other languages such as C++ allows inheritance from multiple classes. • Syntax: class <derived> extends <base>

  9. Inheritance Example • View program listing 8.4class Person (base class) • View program listing 8.5 class Student (derived class) • View demo program, listing 8.6 class InheritanceDemo Sample screen output

  10. Private Class Members • Private class members (i.e. attributes and methods) are NOT inherited. • Thus, cannot be manipulated by the derived classes. • Because they are “private” to the base class. • Likewise, private methods in the base class cannot be used in the derived class. • Unless declared as “public” or “protected”

  11. Derived Classes A class hierarchy

  12. Inheritance Diagrams

  13. Inheritance Diagrams

  14. Constructors in Derived Classes • A derived class does NOT inherit the constructors from the base class • Constructor in a derived class must invoke constructor from base class • Use the reserved word super • Must be first action in the child constructor

  15. The this keyword - Again • Also possible to use the this keyword • Use to call any constructor in the class • When used in a constructor, this calls constructor in the same class • Unlike super, which invokes the constructor of the base class

  16. Programming Example • A derived class of a derived class • View program listing 8.7class Undergraduate • Has all public members of both • Person • Student • This reuses the code in superclasses

  17. Programming Example More details of the class hierarchy

  18. Type Compatibility • In the class hierarchy • Each Undergraduate is also a Student • Each Student is also a Person • An object of a derived class can serve as an object of the base class • Note this is NOT typecasting • An object of a child class can be referenced by a variable of an ancestor type

  19. Type Compatibility • Be aware of the "is-a" relationship • A Studentis aPerson • This is the basis for polymorphism • Another relationship is the "has-a" • A class can contain (as an instance variable) an object of another type • If we specify a date of birth variable for Person – it "has-a" Date object

  20. Polymorphism • From the Greek • “poly” = many • “morph” = form, figure, silhouette • The ability of a class method to do different things based on the object it is acting upon. • Example: Animal.makeNoise(); • Dog: barks • Cat: mews

  21. Polymorphism Two types of polymorphism • Method Overloading: • Multiples implementations of the same method occur in the same class. • Each differs in the number and types of the method arguments. • Java invokes the closest one that matches the actual arguments being passed to the method. • System.out.println ( int ); • System.out.println( char ); • System.out.println ( String ) ; • System.out.println ( boolean );

  22. Polymorphism Two types of polymorphism • Method Overriding: • Multiples implementations of the same method occur in different classes along the same hierarchy. • A child class “overrides” the implementation of a method provided by its base class. • Examples: • Cat.makeNoise( ) overrides Animal.makeNoise( ) • SiameseCat.makeNoise( ) overrides Cat.makeNoise( )

  23. Method Overriding • Go back to listings 8.4, 8.5 and 8.6 • Method writeOutput in Student class • Person class also has writeOutput • Method in child class with same signature overrides method from the parent class • Student objects use the overridden method. • Overridden method must return same type.

  24. Overriding vs. Overloading • Do not confuse overriding with overloading • Overriding takes place in the subclass – new method with same signature • Overloading takes place in the same class – new method with different signature

  25. The final Modifier • It is possible to specify that a method cannot be overridden in a subclass • Add modifier final to the method headingpublic final void specialMethod() • An entire class may be declared final • Thus cannot be used as a base class to derive any other class

  26. Calling an Overridden Method • Reserved word super can also be used to call a base class method in the derived implementation • Useful if the derived implementation is an extension of the base (overridden) one.

  27. Programming Example • Go back to program listing 8.7class Undergraduate • Notice how the writeOutput method invokes the one from its parent class (Student).

  28. The Object Class • Java has a class that is the ultimate ancestor of every class • The class Object • Thus, it is possible to write a method with formal parameter of type Object • Actual parameter in the call can be object of any type • Example: method println(Object theObject)

  29. The Object Class • The Object class has some methods that every Java class inherits • Examples • The equals()method • The toString()method • The toString() method is called when println(theObject)is invoked • Better to define your own toString to handle this.

  30. A Better equals Method • The programmer of a class should override the equals() method of Object • View code of sample override, listing 8.8 public boolean equals (Object theObject)

  31. Abstract Classes and Methods • So far, our base classes provide an implementation for all of its methods. • This means that objects from base classes can actually be created. • e.g. Person p = new Person( ); • What if we cannot provide an implementation for a particular method? • e.g. Animal.makeNoise();

  32. Abstract Classes and Methods • We declare the method as abstract. • No implementation is provided in the base class. • Forces any derived class to implement the method. • Notice the semicolon at the end, meaning that the method has no body. • Different from a method with an empty body.

  33. Abstract Classes and Methods • A class with at least one abstract method becomes an abstract class. • No objects can be created out of an abstract class. • You will derive other classes from it. • And create objects of the derived classes. • e.g. no Animal objects. • but of type Cat, Dog, Bird, Moose, etc.

  34. Abstract Classes and Methods

  35. Extending Abstract Classes Implements the abstract method

  36. Extending Abstract Classes

  37. Revisiting Polymorphism • Method overriding is one of the major sources of polymorphism. • How does it work? • The base class declares a method. • The derived class redefines (overrides) the method. • Must have the same signature.

  38. Revisiting Polymorphism • A variable of any base class can “hold” an object of any of its derived classes. • Functionality is restricted to what the base class can “see” in the derived class. • All members of the base class. • Overridden methods in the derived class. • The implementation in the derived class prevails when the overridden method is invoked. • Called “late binding”

  39. An Example • a2 declared as Animal (base class) • But created as Cat (derived class) • Therefore, “late bound” to Cat at runtime.

  40. Another Example

  41. Another Example Cat.makeNoise( ) SiameseCat.makeNoise( )

  42. An Even More Powerful Example • Suppose the following classes have been created: • Dog extends Animal • Overrides makeNoise( ) as “WofWofWof” • Cow extends Animal • Overrides makeNoise( ) as “Mooooooooo” • Birdie extends Animal • Overrides makeNoise( ) as “Tweet Tweet”

  43. An Even More Powerful Example

  44. An Even More Powerful Example This is the program output:

More Related