Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming: Polymorphism Chapter 10.

Similar presentations


Presentation on theme: "Object-Oriented Programming: Polymorphism Chapter 10."— Presentation transcript:

1 Object-Oriented Programming: Polymorphism Chapter 10

2 2 Problem with Subclasses Given the class hierarchy below Consider the existence of a ______________ function for each subclass Consider also an ____________________ to the superclass (which can also point to various objects of the subclasses) How do you specify _________ draw statement to be called when using the references? Shape class hierarchy Cricle Right TriangleIsosceles Triangle Triangle Square Rectangle Shape

3 3 Invoking Superclass Methods from Subclass Objects Recall the point and circle hierarchy –Note : Assignment of superclass __________ to superclass variable –Assignment of ______________ reference to subclass variable No surprises … but the "is-a" relationship makes possible –Assignment of ____________ reference to ______________ variable See Figure 10.1Figure 10.1

4 4 Using Superclass References with Subclass-Type Variables Suppose we have a superclass object Point3 point = new Point3 (30, 40); Then a subclass reference Circle4 circle; It would be illegal to have the subclass reference aimed at the superclass object ____________________;

5 5 Subclass Method Calls via Superclass-Type Variables Consider aiming a subclass reference at a superclass object –If you use this to access a __________________________ it is illegal Note Figure 10.3Figure 10.3 –Lines 22 - 23

6 6 Summary of Legal Assignments between Super- and Subclass Variables Assigning superclass reference to superclass-type variable ____________ Assigning subclass reference to subclass-type variable ____________ Assigning subclass object's reference to superclass type-variable, ________________ –A circle "is a" point –Only possible to invoke ___________ methods ________________ assign superclass reference to subclass-type variable –You can cast the superclass reference to a subclass type (explicitly)

7 7 Polymorphism Examples Suppose designing video game Superclass SpaceObject –Subclasses Martian, SpaceShip, LaserBeam –Contains method draw To refresh screen –Send draw message to each object –________________ has “many forms” of results Easy to add class Mercurian –Extends SpaceObject –Provides its own _________________________ Programmer does not need to change code –Calls draw regardless of object’s type –Mercurian objects “plug right in”

8 8 Polymorphism Enables programmers to deal in _______________ –Let execution-time environment handle specifics Able to command objects to behave in manners appropriate to those objects –Don't need to know ___________ of the object –Objects need only to belong to same _________________ hierarchy

9 9 Polymorphism Promotes ______________ New objects types can respond to existing _______________________ –Can be incorporated into a system without modifying base system Only client code that ________________ the new objects must be modified –To accommodate new types

10 10 Abstract Classes and Methods Abstract classes –Are _________________ (called abstract superclasses) –Cannot be __________________ –Incomplete subclasses fill in "missing pieces" _______________ classes –Can be instantiated –Implement every ____________ they declare –Provide specifics

11 11 Abstract Classes and Methods Application example Abstract class Shape –Declares draw as abstract method public abstract void draw(); Circle, Triangle, Rectangle extends Shape –Each ___________________ draw Each object can _________________

12 12 Abstract Classes and Methods Purpose of an abstract class –Declare common ___________ … –Declare common ______________ of classes in a class hierarchy Contains one or more _____________ methods –Subclasses must _______________ Instance variables, concrete methods of abstract class –subject to normal rules of inheritance

13 13 Abstract Classes and Methods Abstract classes not required, but reduce client code _________________ To make a class abstract –Declare with ____________ abstract –Contain one or more abstract methods public abstract void draw(); –Abstract methods ______________, must be overridden

14 14 Inheriting Interface and Implementation Make abstract superclass Shape _______________ method (must be implemented) –getName, print –Default implementation does not make sense Methods may be overridden –getArea, getVolume Default implementations return 0.0 –If not overridden, uses ___________ default implementation Subclasses Point, Circle, Cylinder

15 15 Circle Cylinder Point Shape Polymorphic Interface For The Shape Hierarchy Class 0.0 abstractdefault Object implement 0.0 "Point"[x,y] πr2πr2 0.0"Circle" center=[x,y]; radius=r 2 π r 2 +2 π rh πr2hπr2h "Cylinder" center=[x,y]; radius=r; height=h getAreatoStringgetNamegetVolume Shape Point Circle Cylinder

16 16 Implementation vs. Interface Inheritance Implementation Inheritance Functionality ______ in the hierarchy Each new subclass inherits one or more _____ declared in superclass Subclass uses superclass __________ Interface Inheritance ________ lower in hierarchy Superclass specifies one or more ___________ methods Must be _______ for each class in hierarchy ________ for subclass- specific implementations

17 17 Source Code for Shape Superclass Hierarchy Shape superclass, Figure 10.6Figure 10.6 –Note __________ method, getName Point subclass, Figure 10.7Figure 10.7 –Note, extends Shape, _________ of getName Circle subclass, Figure 10.8Figure 10.8 –Note extends Point, override of getArea, getName, and toString Cylinder subclass, Figure 10.9Figure 10.9 –Note ________ Circle, override of getArea, getName, and toString

18 18 Source Code for Shape Superclass Hierarchy Driver program to demonstrate, Figure 10.10 Figure 10.10 –Note array of __________________ Output of program

19 19 Polymorphic Payroll System Class hierarchy for polymorphic payroll application Employee SalariedEmployeeHourlyEmployeeCommissionEmployee BasePlusCommissionEmployee

20 20 Polymorphic Payroll System Abstract superclass, Employee, Figure 10.12 Figure 10.12 –Note ____________ class specification, abstract earnings method Abstract subclass, SalariedEmployee, Figure 10.13 Figure 10.13 –Note ____________ Employee, override of earnings method Look at other subclasses in text, pg 461 … –Note here also the ___________ of earnings

21 21 Polymorphic Payroll System View test program, Figure 10.17Figure 10.17 –Note array of superclass _____________ which are pointed at various subclass objects –Note generic calls of ____________ method –Note use of instantceof operator –Consider use of _____________ (line 37) –Note use of getClass().getName() method Gives access to the name of the class

22 22 Polymorphic Payroll System Output of the payroll testing program Note results of getClass().getName() calls

23 23 Why Use Interfaces Java has _____________ inheritance, only This means that a child class inherits from only one parent class Sometimes _____________ inheritance would be convenient _______________ give Java some of the advantages of multiple inheritance without incurring the disadvantages

24 24 What is an Interface? An interface is a collection of ____________ and ______________ declarations The method declarations do not include an _____________ (there is no method body) A child class that extends a parent class can also ______________ an interface to gain some additional behavior

25 25 Rules for Interfaces A method in an interface cannot be made _________________ When a class definition implements an interface: –It ________________ each method in the interface –Each method must be _________ (even though the interface might not say so) –_____________________ from the interface can be used as if they had been defined in the class (They should not be re-defined in the class)

26 26 Creating and Using Interfaces Recall comments on interface inheritance Declaration begins with ____________ keyword ____________ implement an interface (and its methods) Contains public ____________ methods –Classes (that implement the interface) must implement these methods

27 27 Creating and Using Interfaces Example of an interface, Figure 10.18 Contrast with line 4 of Figure 10.6Figure 10.6 Note also, no implementation of ___________ 1 // Fig. 10.18: Shape.java 2 // Shape interface declaration. 3 4 public interface Shape { 5 public double getArea(); // calculate area 6 public double getVolume(); // calculate volume 7 public String getName(); // return shape name 8 9 } // end interface Shape 1 // Fig. 10.18: Shape.java 2 // Shape interface declaration. 3 4 public interface Shape { 5 public double getArea(); // calculate area 6 public double getVolume(); // calculate volume 7 public String getName(); // return shape name 8 9 } // end interface Shape

28 28 Creating and Using Interfaces View implementation of the interface Shape, Figure 10.19 Note the following: Figure 10.19 –Line 4 public class Point extends Object implements Shape { … –Declaration of ____________________ –Declaration of _____________ methods getArea, getVolume, and getName

29 29 Creating and Using Interfaces All the same "_____________" relationships apply when an interface inheritance is used Objects of any class that extend the _____________ are also objects of the interface type –Circle extends Point, thus it is-a Shape _______________ interfaces are possible –Comma-separated list used in declaration

30 30 Nested Classes Top-level classes –Not declared inside a class or a method __________________ classes –Declared inside other classes –____________ classes Non-static nested classes Demonstrated in Figure 10.22Figure 10.22 –Run the program –AudioAudio

31 31 Mentioned In The Audio Inheritance HierarchyHierarchy Panes of a JFrame – Object Component Container Window Frame JFrame

32 32 Mentioned In The Audio Three things required when performing events 1.__________ the interface 2.Register the ________________ 3.Specifically implement the _________________ methods

33 33 Nested Classes An inner class is allowed to directly access its inner class's ___________________ When this used in an inner class –Refers to ______________ object To access outer-class using this –Precede this with _______________ name

34 34 Nested Classes Anonymous inner class –Declared inside a method of a class –Has no ____________________ Inner class declared inside a method –Can access outer class's _____________ –Limited to local variables of method in which declared Note use of anonymous inner class, Figure 10.23Figure 10.23

35 35 Notes on Nested Classes _______________ class that contains nested class –Results in separate _______________ file –OuterClassName$InnerClassName.class Inner classes with names can be declared as –public, protected, private or package access

36 36 Notes on Nested Classes Access outer class’s __________ reference OuterClassName.this Outer class is responsible for creating inner class objects OuterClassName.InnerClassName innerRef = ref.new InnerClassName(); Nested classes can be declared _________ –Object of outer class need not be created –Static nested class ____________________ to outer class's non-static members

37 37 Type-Wrapper Classes for Primitive Types Each __________ type has a type wrapper –Character, Byte, Integer, Boolean, etc. Enable to represent ________ as Object –Primitive values can be processed ____________ using wrapper classes Declared as final Many methods are declared static


Download ppt "Object-Oriented Programming: Polymorphism Chapter 10."

Similar presentations


Ads by Google