Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.

Slides:



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

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS 211 Inheritance AAA.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
1 Polymorphism  A reference can be polymorphic, which can be defined as "having many forms" obj.doIt();  This line of code might execute different methods.
© 2006 Pearson Education Chapter 7: Inheritance Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
Chapter 7: Inheritance Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java Software.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved Polymorphism Polymorphism is an object-oriented concept that allows.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved Polymorphism Polymorphism is an object-oriented concept that allows.
Inheritance in the Java programming language J. W. Rider.
© 2004 Pearson Addison-Wesley. All rights reserved April 17, 2006 Polymorphism (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
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.
Abstract Classes. Review PA – 3 Design Considerations /Web/CS239/programs/pa3/draft.php ?harrisnlCS239
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved Polymorphism Polymorphism is an object-oriented concept that allows.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Coming up: Inheritance
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Chapter 9 Polymorphism 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
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.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Polymorphism November 27, 2006 ComS 207: Programming I (in Java)
Chapter 9 Polymorphism.
Inheritance and Polymorphism
Polymorphism.
Object-Oriented Programming: Polymorphism
Lecture 14 - Abstract Classes
Designing for Inheritance
Chapter 9: Polymorphism and Inheritance
Advanced Java Topics Chapter 9
Week 6 Object-Oriented Programming (2): Polymorphism
Lecture 14- Abstract Classes
Overriding Methods & Class Hierarchies
Java Inheritance.
Chapter 9 Polymorphism.
Chapter 8 Inheritance Part 2.
Lecture 0311 – Polymorphism
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein

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.  See examplesexamples

Polymorphism  A reference can be polymorphic, which can be defined as "having many forms" obj.doIt();  This line of code might execute different methods at different times if the object that obj points to changes

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

Polymorphism via Inheritance  It is the type of the object being referenced, not the reference 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: StaffMember ExecutiveHourly VolunteerEmployee See page 485 for the full UML diagram

Polymorphism via Inheritance  Now consider the task of paying all employees  See Firm.java Firm.java  See Staff.java Staff.java  See StaffMember.java StaffMember.java  See Volunteer.java Volunteer.java  See Employee.java Employee.java  See Executive.java Executive.java  See Hourly.java Hourly.java

Polymorphism in another view StaffMember you Volunteer StaffMember me Executive

Object Class  The Granddaddy of all classes.  We override the toString and equals methods commonly in creating our own classes.  Question – Is it possible for us to write code that will do something different based on the class of the object instead of the class in which the variable is declared. Like, can we prevent the run time errors we saw in the Executive vs Hourly classes.

Class class  Object class has a getClass method that returns the Class of the object.  The Class class has a getName method which returns a String equivalent to the name of the Class.  Using these two, we could write code that will do one thing for one class of object and another for a different class of object.

Meanings of Polymorphism  In this course:  At run-time, a variable may "take the form of" any of its descendants.  Related Concepts: Dynamic binding

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)  top compartment – class name  middle compartment – class attributes  name : type  bottom compartment – class methods  name (parameterList types) : return type

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 again at the StaffMember 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?

Another example  Shapes - n11/DesigningClasses.pdfn11/DesigningClasses.pdf  What should the hierarchy look like?  Which classes should be abstract?  Which concrete?  What services should be required (made abstract)?  n11/ShapeUML.pdf n11/ShapeUML.pdf

Finished product  Shape.java Shape.java  TwoDimensionalShape.java TwoDimensionalShape.java  ThreeDimensionalShape.java ThreeDimensionalShape.java  Sphere.java Sphere.java  Rectangle.java Rectangle.java