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 Software Reuse One goal of object-oriented programming is to promote reuse of existing classes Sometimes you have a class that does some of what you need –The JFrame class provides the basic functionality of a window for a GUI program Inheritance allows you to create a subclass with all the functionality of the existing class (superclass) –You can add new functionality to the subclass by adding new methods and instance data –You can modify the behavior of the subclass by overriding superclass methods

3 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 3 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 actual 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

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 a method body for the abstract methods listed 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 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 class 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 Polymorphism Example 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.

7 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 7 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. –Using public members violates encapsulation principles –protected data members and methods are visible and accessible in all descendant classes.

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

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

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

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

12 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 12 Modeling Two Types of Students 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

13 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 13 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.

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

15 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 15 Creating the roster Array Maintain a class roster using an array of Student references –Each element can be instantiated as a Student, UndergraduateStudent or GraduateStudent 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 getCourseGrade from GraduateStudent class is executed. –If roster[i] refers to an UndergraduateStudent, then getCourseGrade from UndergraduateStudent class is executed. for (int i = 0; i < numberOfStudents; i++) { roster[i].getCourseGrade(); }

17 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 17 Inheritance and Constructors Constructors are not inherited by subclasses. When any constructor is called –there is an implicit call to the default constructor from the superclass. –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 other methods super.toString();

18 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 18 Abstract Classes An interface consists of a list of methods that aren't implemented –all methods in an interface are abstract Occasionally, a superclass will have some methods that it doesn't make sense to implement –think about shapes - can't define draw until you know what the shape is We can make the superclass abstract –Use abstract keyword in method header –Some methods can be implemented –Label unimplemented methods abstract

19 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 19 Abstract Classes 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 Approaches to Student Classes Student is abstract -Need subclass for each type of student Student is instantiable –Use superclass for default behavior

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.

22 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 22 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

23 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 23 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++; }


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