Download presentation
Presentation is loading. Please wait.
1
Lecture 0311 – Polymorphism
Nancy Harris
2
New Teams Eskridge Irons Fisher-Duke Onat Bardas Deck Naylor Foster
Coradazzi Heatwole Olvera McMillen Ernst Hubbard Hittie Cottingham Field, Kevin Jiggetts Sheppard Moran Greene Staleva Underwood Shami Moomau Gagon Tucker Galaska Sibley Mawyer Goldenberg Frye Judson Allen Bailey Hart Kelchner Russell Copeland Mersiovsky Tessitore Miles Zhang Massetti
3
Inheritance - Preventing a Method from Being Overridden
The final modifier will prevent the overriding of a superclass method in a subclass. public final void message() If a subclass attempts to override a final method, the compiler generates an error. This ensures that a particular superclass method is used by subclasses rather than a modified version of it.
4
Protected Members Using protected instead of private makes some tasks easier. However, any class that is derived from the class, or is in the same package, has unrestricted access to the protected member. It is always better to make all fields private and then provide public methods for accessing those fields. If no access specifier for a class member is provided, the class member is given package access by default. Any method in the same package may access the member.
5
Polymorphism from the Greek poly = many morph = forms
6
We have seen polymorphism
Think clocks. You can create a Clock or an AlarmClock. You can create a Clock and instantiate an AlarmClock. The method used (like the updateTime method) will be based on the object type. But the object declaration determines which methods are available to us.
7
Polymorphism Polymorphic references are resolved at run time; this is called dynamic binding Careful use of polymorphic references can lead to elegant, robust software designs Polymorphism can be accomplished using inheritance or using interfaces(to be discussed later)
8
References and Inheritance
An object reference can refer to an object of its class, or to an object of any class related to it by inheritance For example, if the Holiday class is used to derive a child class called Christmas, then a Holiday reference could be used to point to a Christmas object Holiday day; day = new Christmas(); Holiday Christmas
9
References and Inheritance
An Object reference can be used to refer to any object An ArrayList is designed to hold Object references See API for ArrayList But we have also seen that we can force the ArrayList to only hold objects of a particular type.
10
Polymorphism via Inheritance
It is the type of the object being referenced, not the declared type, that determines which method is invoked Suppose the Holiday class has a method called celebrate, and the Christmas class overrides it Now consider the following invocation: day.celebrate(); If day refers to a Holiday object, it invokes the Holiday version of celebrate; if it refers to a Christmas object, it invokes the Christmas version
11
Polymorphism via Inheritance
Consider the following class hierarchy: PassFailActivity PassFailExam GradedActivity FinalExam
12
Copyright © 2005, Pearson Addison-Wesley. All rights reserved.
Polymorphism A GradedActivities variable can be used to reference a FinalExam object. GradedActivity exam = new FinalExam(50, 7); This statement creates a FinalExam object and stores the object’s address in the exam variable. This is an example of polymorphism. The term polymorphism means the ability to take many forms. In Java, a reference variable is polymorphic because it can reference objects of types different from its own, as long as those types are subclasses of its type. Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.
13
Copyright © 2005, Pearson Addison-Wesley. All rights reserved.
Polymorphism Other legal polymorphic references: GradedActivity exam1 = new FinalExam(50, 7); GradedActivity exam2 = new PassFailActivity(70); GradedActivity exam3 = new PassFailExam(100, 10, 70); The GradedActivity class has three methods: setScore, getScore, and getGrade. A GradedActivity variable can be used to call only those three methods. GradedActivity exam = new PassFailExam(100, 10, 70); System.out.println(exam.getScore()); // This works. System.out.println(exam.getGrade()); // This works. System.out.println(exam.getPointsEach()); // ERROR! Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.
14
Polymorphism via Inheritance
Consider all kinds of grading See Polymorphic.java See GradedActivity.java See PassFailActivity.java See FinalExam.java See PassFailExam.java
15
Polymorphism in Java Class Membership
The compiler uses the declared class At run-time an object knows its actual class Search Order: Think of a method call as being a “message” to the object. The message may contain parameters or it may not. The message is always asking the object to do something. If a "message" is sent to an object of the derived class then the derived class is searched (for the existence of such a method) first and the base class is searched second. (Note: The search will move up the class hierarchy until found.) If the "message" is sent to an object of the base class then only the base class is searched. At compile time we know which messages are valid; at run time we use the particular version of the message that corresponds to the object reacting to the message.
16
Taking it a step further
Abstract classes – templates for class families
17
Rules for Abstract Classes
An abstract class cannot be instantiated A class that can have instances is said to be concrete An abstract class provides a prototype for other classes to follow
18
Subclasses of an Abstract Class
will inherit the variables and methods of the abstract class will have the same basic characteristics are free to redefine variables and methods and add new ones must override any abstract methods of its parent (unless it itself is abstract).
19
Abstract Classes generally contain at least one abstract method
are any classes containing at least one abstract method can contain non-abstract methods If there is an abstract method, then the class must be declared as abstract, but… If a class is declared as abstract it may or may not have non-abstract or abstract methods.
20
Abstract Methods have the word abstract in their declaration
do not have a body end their declarations with a semi-colon must be overridden in concrete children are generally methods whose bodies will change from one subclass to another
21
Standard UML Notation + public - private # protected
{abstract} (name is italicized)
22
Abstract Classes The child of an abstract class must override the abstract methods of the parent, or it too will be considered abstract An abstract method cannot be defined as final (because it must be overridden) or static (because it has no definition yet) The use of abstract classes is a design decision – it helps us establish common elements in a class that is too general to instantiate
23
Let’s look at the Student class
What will happen if: We remove the word abstract in the class header? We change the abstract method to a non-abstract method? We try to instantiate a StaffMember object? We decide not to “pay” Volunteers? We create a new class from the StaffMember object which is an abstract class also?
24
Student class family Student.java CompSciStudent.java
CompSciStudentDemo.java
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.