Presentation is loading. Please wait.

Presentation is loading. Please wait.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.

Similar presentations


Presentation on theme: "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces."— Presentation transcript:

1 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces Inheritance

2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 2 Polymorphism Roughly, this means many forms In Java programs, what it means is that a variable can refer to different kinds of objects at different times –Calling a method calls the version of the method that is appropriate for the type of the object. Two approaches to class polymorphism –Implement an interface –Use inheritance to extend a class Overloading methods is another form of polymorphism

3 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 3 Polymorphism Polymorphism allows a single variable to refer to objects from different subclasses in the same inheritance hierarchy If a Cat is a Pet and a Dog is a Pet, then the following statements are valid: Pet myPet; myPet = new Dog();... myPet = new Cat(); Both inheritance and interfaces support polymorphism.

4 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 4 Interfaces A Java interface includes only constants and abstract methods. A Java interface specifies a behavior. –Any class can implement the interface. –A class can implement any number of interfaces. A class implements an interface by providing the method body to the abstract methods stated in the interface. public class ImplementingClass implements InterfaceName { // provide definitions for all the methods in the interface }

5 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 5 Inheritance Inheritance is a mechanism which allows a class to re- use the methods and variables of another class. Every class inherits the methods of the Object class –toString, equals, clone,... –We can override an inherited method to make it behave appropriately for the class that inherited it you have been overriding the toString method in your classes Use the extends keyword to indicate that a class inherits from another class public Subclass extends Superclass {...} –A class can only extend one class (single inheritance)

6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 6 Visibility Modifiers We've already seen two visibility modifiers –public data members and methods are accessible to everyone. –private data members and methods are accessible only to instances of the class. With inheritance, we need a third visibility modifier –Private members of the superclass are not visible within a subclass. –Making all members public would violate encapsulation principles –protected data members and methods are visible and accessible to the instances of the class and any descendant classes.

7 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 7 Inheritance and Member Accessibility We use the following visual representation of inheritance to illustrate data member accessibility.

8 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 8 The Effect of Three Visibility Modifiers

9 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 9 Accessibility of Super from Sub Everything except the private members of the Super class is visible from a method of the Sub class.

10 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 10 Case Study Suppose we want implement a class roster that contains both undergraduate and graduate students. Each student’s record will contain his or her name, three test scores, and the final course grade. The formula for determining the course grade is different for graduate students than for undergraduate students.

11 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 11 Modeling Two Types of Students There are three ways to design the classes to model undergraduate and graduate students. –We can define two unrelated classes, one for undergraduates and one for graduates. –We can model the two kinds of students by using classes that are related in an inheritance hierarchy. Superclass can be either abstract or concrete –We can define a Student interface and implement it with classes to represent graduate and undergraduate students

12 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 12 Classes for the Class Roster For the Class Roster sample, we design three classes: –Student –UndergraduateStudent –GraduateStudent The Student class will incorporate behavior and data common to both UndergraduateStudent and GraduateStudent objects. The UndergraduateStudent class and the GraduateStudent class will each contain behaviors and data specific to their respective objects.

13 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 13 Inheritance Hierarchy

14 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 14 Polymorphism with inheritance Two ways to provide polymorphism using inheritance –Override an inherited method to provide behavior specific to a particular subclass –Implement an abstract method to provide different behavior for different subclasses

15 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 15 Creating the roster Array We can maintain a class roster using an array, cotaining objects from the Student, UndergraduateStudent, and GraduateStudent classes. Student [] roster = new Student[40]; roster[0] = new GraduateStudent(); roster[1] = new UndergraduateStudent(); roster[2] = new UndergraduateStudent();

16 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 16 Sample Polymorphic Message To compute the course grade using the roster array, we execute We don't need to know which class any particular object belongs to. –If roster[i] refers to a GraduateStudent, then the computeCourseGrade method of the GraduateStudent class is executed. –If roster[i] refers to an UndergraduateStudent, then the computeCourseGrade method of the UndergraduateStudent class is executed. for (int i = 0; i < numberOfStudents; i++) { roster[i].computeCourseGrade(); }

17 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 17 The instanceof Operator If necessary, the instanceof operator can help us learn the class of an object. The following code counts the number of undergraduate students. int undergradCount = 0; for (int i = 0; i < numberOfStudents; i++) { if ( roster[i] instanceof UndergraduateStudent ) { undergradCount++; }

18 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 18 Inheritance and Constructors Constructors of a superclass are not inherited by subclasses. All constructors –By default, there is an implicit call to the default constructor from the superclas. –You can explicitly call either the default constructor or another one using super(); The keyword super can also be used to explicitly call the superclass version of a method. super.toString();

19 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 19 Definition: Abstract Class An abstract class is a class –defined with the modifier abstract –that contains an abstract method –that does not provide an implementation of an inherited abstract method An abstract method is a method with the keyword abstract, and it ends with a semicolon instead of a method body. –Private methods and static methods may not be declared abstract. No instances can be created from an abstract class. –Just as no instances can be created from an interface.

20 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 20 Two Approaches to Student Classes 1.Student must be Undergraduate or Graduate –We define the Student class as abstract so that no instances may be created. 2.There may be Students which are neither Undergraduate nor Graduate. a)We can make the Student class instantiable. b)We can leave the Student class abstract and add a third subclass, OtherStudent, to handle a student who does not fall into the UndergraduateStudent or GraduateStudent categories.

21 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 21 Inheritance versus Interface The Java interface is used to share common behavior (only method headers) among the instances of different classes. –All classes that implement comparable have compareTo –All classes that implement ActionListener have actionPerformed Inheritance is used to share common code (including both data members and methods) among the instances of related classes. If an entity A is a specialized form of another entity B, then model them by using inheritance. –It should make sense to say an A is a B –Declare A as a subclass of B.


Download ppt "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces."

Similar presentations


Ads by Google