Lecture 0311 – Polymorphism

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Abstract Classes.
1 Inheritance and Polymorphism Inheritance (Continued) Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
ECE122 L22: Polymorphism Using Inheritance April 26, 2007 ECE 122 Engineering Problem Solving with Java Lecture 22 Polymorphism using Inheritance.
Inheritance, Polymorphism, and Virtual Functions
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Inheritance Chapter 11.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance in the Java programming language J. W. Rider.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
11-1 Chapter.9 Classes & Objects: Inheritance –What Is Inheritance? –Calling the Superclass Constructor –Overriding Superclass Methods –Protected Members.
AN INTRODUCTION TO INHERITANCE. In your group Define 5 attributes that you would use to describe persons. Define 3 different attributes that can describe.
© 2012 Pearson Education, Inc. All rights reserved. Chapter 11: Inheritance Starting Out with Java: From Control Structures through Data Structures Second.
Chapter Topics Chapter 10 discusses the following main topics:
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Inheritance Starting Out with Java: From Control Structures.
Inheriatance. 9-2 What is Inheritance? Generalization vs. Specialization Real-life objects are typically specialized versions of other more general objects.
Abstract Classes. Review PA – 3 Design Considerations /Web/CS239/programs/pa3/draft.php ?harrisnlCS239
More on Inheritance Chapter 11 Continued. Reminders Overloading – different signatures Overriding – same signatures Preventing overriding – use final.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
11-2  What Is Inheritance?  Calling the Superclass Constructor  Overriding Superclass Methods  Protected Members  Chains of Inheritance  The Object.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects.
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
© 2010 Pearson Addison-Wesley. All rights reserved. AN INTRODUCTION TO INHERITANCE.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapter 11 Polymorphism. Contents I. Polymorphism 1. Polymorphism 2. Polymorphism and Dynamic Binding 3. The “is-a” Relationship Does Not Work in Reverse.
Polymorphism November 27, 2006 ComS 207: Programming I (in Java)
Chapter 9 Polymorphism.
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
Inheritance, Polymorphism, and Virtual Functions
by Tony Gaddis and Godfrey Muganda
Polymorphism.
Object Oriented Programming
Lecture 14 - Abstract Classes
Designing for Inheritance
An introduction to inheritance
Starting Out with Java: From Control Structures through Objects
Java Programming Language
Chapter 9: Polymorphism and Inheritance
Advanced Java Topics Chapter 9
Inheritance, Polymorphism, and Virtual Functions
Week 6 Object-Oriented Programming (2): Polymorphism
Polymorphism, Abstract Classes & Interfaces
Lecture 14- Abstract Classes
CSE 1030: Implementing GUI Mark Shtern.
Abstract Classes An abstract class is a kind of ghost class. It can pass along methods and variables but it can’t ever be instantiated itself. We can.
Java – Inheritance.
Polymorphism Polymorphism - Greek for “many forms”
Overriding Methods & Class Hierarchies
Java Inheritance.
Fundaments of Game Design
Chapter 8 Inheritance Part 2.
Final and Abstract Classes
Chapter 11: Inheritance Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Lecture 0311 – Polymorphism Nancy Harris

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

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.

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.

Polymorphism from the Greek poly = many morph = forms

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.

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)

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

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.

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

Polymorphism via Inheritance Consider the following class hierarchy: PassFailActivity PassFailExam GradedActivity FinalExam

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.

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.

Polymorphism via Inheritance Consider all kinds of grading See Polymorphic.java See GradedActivity.java See PassFailActivity.java See FinalExam.java See PassFailExam.java

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.

Taking it a step further Abstract classes – templates for class families

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

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).

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.

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

Standard UML Notation + public - private # protected {abstract} (name is italicized)

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

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?

Student class family Student.java CompSciStudent.java CompSciStudentDemo.java