Polymorphism November 27, 2006 ComS 207: Programming I (in Java)

Slides:



Advertisements
Similar presentations
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Advertisements

Chapter 8 Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
CS 211 Inheritance AAA.
Chapter 8 Inheritance 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
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
More Inheritance Abstract Classes Interfaces Briana B. Morrison CSE 1302C Spring 2010.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved9-2 Polymorphism Polymorphism (having many forms) is an object- oriented concept.
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.
Chapter 9 Polymorphism 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter Day 21. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 20 Problem set 4 Posted  10 problems from chapters 7 & 8  Due Nov 21.
Chapter 8 Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
© 2004 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 : Inheritance Intermediate Java Programming Summer 2007.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
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.
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.
Chapter 9: Polymorphism Coming up: Creating Objects Revisited.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved Polymorphism Polymorphism is an object-oriented concept that allows.
© 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.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/23 Outline Creating Subclasses Overriding Methods Class Hierarchies.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved Polymorphism Polymorphism is an object-oriented concept that allows.
© 2004 Pearson Addison-Wesley. All rights reserved November 30, 2007 Exceptions ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
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.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
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.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
© 2004 Pearson Addison-Wesley. All rights reserved November 14, 2007 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Chapter 9 Polymorphism.
03/10/14 Inheritance-2.
Polymorphism.
Chapter 7: Inheritance Java Software Solutions
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
Designing for Inheritance
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
Review for the Final Exam
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
Overriding Methods & Class Hierarchies
Exceptions April 19, 2006 ComS 207: Programming I (in Java)
Chapter 8 Inheritance.
Chapter 9 Polymorphism.
Chapter 8 Inheritance Part 2.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Polymorphism November 27, 2006 ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor: Alexander Stoytchev © 2004 Pearson Addison-Wesley. All rights reserved

Final Exam Time: Tuesday Dec 12 @ 7:00-9:00 p.m. Location: same as midterms, i.e., Hoover 2055. © 2004 Pearson Addison-Wesley. All rights reserved

Quick Review of Last Lecture © 2004 Pearson Addison-Wesley. All rights reserved

Method Overloading The compiler determines which method is being invoked by analyzing the parameters [signature 1] tryMe: int float tryMe(int x) { return x + .375; } [signature 2] tryMe: int, float float tryMe(int x, float y) { return x*y; } © 2004 Pearson Addison-Wesley. All rights reserved

Method Overriding Same Signatures Different Method Bodies public class Parent { public float tryMe(int x) return x + .375; } Same Signatures public class Child extends Parent { public float tryMe(int x) return x*x; } Different Method Bodies © 2004 Pearson Addison-Wesley. All rights reserved

Overriding Methods A child class can override the definition of an inherited method in favor of its own The new method must have the same signature as the parent's method, but can have a different body The type of the object executing the method determines which version of the method is invoked © 2004 Pearson Addison-Wesley. All rights reserved

Overriding A method in the parent class can be invoked explicitly using the super reference If a method is declared with the final modifier, it cannot be overridden The concept of overriding can be applied to data and is called shadowing variables Shadowing variables should be avoided because it tends to cause unnecessarily confusing code © 2004 Pearson Addison-Wesley. All rights reserved

Overloading vs. Overriding Overloading deals with multiple methods with the same name in the same class, but with different signatures Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature © 2004 Pearson Addison-Wesley. All rights reserved

Overloading vs. Overriding Overloading lets you define a similar operation in different ways for different parameters Overriding lets you define a similar operation in different ways for different object types © 2004 Pearson Addison-Wesley. All rights reserved

Class Hierarchies A child class of one parent can be the parent of another child, forming a class hierarchy Business KMart Macys ServiceBusiness Kinkos RetailBusiness © 2004 Pearson Addison-Wesley. All rights reserved

Class Hierarchies Two children of the same parent are called siblings Common features should be put as high in the hierarchy as is reasonable An inherited member is passed continually down the line Therefore, a child class inherits from all its ancestor classes There is no single class hierarchy that is appropriate for all situations © 2004 Pearson Addison-Wesley. All rights reserved

Object – the mother of all objects in Java © 2004 Pearson Addison-Wesley. All rights reserved

The Object Class The equals method of the Object class returns true if two references are aliases We can override equals in any class to define equality in some more appropriate way As we've seen, the String class defines the equals method to return true if two String objects contain the same characters The designers of the String class have overridden the equals method inherited from Object in favor of a more useful version © 2004 Pearson Addison-Wesley. All rights reserved

© 2004 Pearson Addison-Wesley. All rights reserved [http://www.holtsoft.com/java/resources/inherit_tutorial/Inheritance/methods.jpg]

Abstract Classes An abstract class is a placeholder in a class hierarchy that represents a generic concept An abstract class cannot be instantiated We use the modifier abstract on the class header to declare a class as abstract: public abstract class Product { // contents } © 2004 Pearson Addison-Wesley. All rights reserved

public abstract class Animal { abstract void makeSound(); } public class Cow extends Animal { public void makeSound() { System.out.println("Moo-Moo"); } } public class Dog extends Animal { public void makeSound() { System.out.println(“Wuf-Wuf"); } } public class Duck extends Animal { public void makeSound() { System.out.println(“Quack-Quack"); } } © 2004 Pearson Addison-Wesley. All rights reserved

Abstract Classes An abstract class often contains abstract methods with no definitions (like an interface) Unlike an interface, the abstract modifier must be applied to each abstract method Also, an abstract class typically contains non- abstract methods with full definitions A class declared as abstract does not have to contain abstract methods -- simply declaring it as abstract makes it so © 2004 Pearson Addison-Wesley. All rights reserved

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 or static The use of abstract classes is an important element of software design – it allows us to establish common elements in a hierarchy that are too generic to instantiate © 2004 Pearson Addison-Wesley. All rights reserved

Other Stuff From Chapter 8 © 2004 Pearson Addison-Wesley. All rights reserved

Interface Hierarchies Inheritance can be applied to interfaces as well as classes That is, one interface can be derived from another interface The child interface inherits all abstract methods of the parent A class implementing the child interface must define all methods from both the ancestor and child interfaces Note that class hierarchies and interface hierarchies are distinct (they do not overlap) © 2004 Pearson Addison-Wesley. All rights reserved

This example shows how multiple inheritance can be faked in java © 2004 Pearson Addison-Wesley. All rights reserved [http://www.vsj.co.uk/pix/articleimages/may05/javathread3.jpg]

Visibility Cartoon [http://faculty.juniata.edu/kruse/cs2/vis.gif] © 2004 Pearson Addison-Wesley. All rights reserved [http://faculty.juniata.edu/kruse/cs2/vis.gif]

Visibility Revisited It's important to understand one subtle issue related to inheritance and visibility All variables and methods of a parent class, even private members, are inherited by its children As we've mentioned, private members cannot be referenced by name in the child class However, private members inherited by child classes exist and can be referenced indirectly © 2004 Pearson Addison-Wesley. All rights reserved

Visibility Revisited Because the parent can refer to the private member, the child can reference it indirectly using its parent's methods The super reference can be used to refer to the parent class, even if no object of the parent exists © 2004 Pearson Addison-Wesley. All rights reserved

Chapter 9 Section 9.1 & 9.2

Polymorphism Polymorphism is an object-oriented concept that allows us to create versatile software designs Chapter 9 focuses on: defining polymorphism and its benefits using inheritance to create polymorphic references using interfaces to create polymorphic references using polymorphism to implement sorting and searching algorithms additional GUI components © 2004 Pearson Addison-Wesley. All rights reserved

Polymorphism in Nature © 2004 Pearson Addison-Wesley. All rights reserved [http://www.blackwellpublishing.com/ridley/images/h_erato.jpg]

Binding Consider the following method invocation: obj.doIt(); At some point, this invocation is bound to the definition of the method that it invokes If this binding occurred at compile time, then that line of code would call the same method every time However, Java defers method binding until run time -- this is called dynamic binding or late binding Late binding provides flexibility in program design © 2004 Pearson Addison-Wesley. All rights reserved

Polymorphism The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects at different points in time The method invoked through a polymorphic reference can change from one invocation to the next All object references in Java are potentially polymorphic © 2004 Pearson Addison-Wesley. All rights reserved

Polymorphism Suppose we create the following reference variable: Occupation job; Java allows this reference to point to an Occupation object, or to any object of any compatible type This compatibility can be established using inheritance or using interfaces Careful use of polymorphic references can lead to elegant, robust software designs © 2004 Pearson Addison-Wesley. All rights reserved

Polymorphism via Inheritance © 2004 Pearson Addison-Wesley. All rights reserved

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 class called Christmas, then a Holiday reference could be used to point to a Christmas object Holiday Christmas Holiday day; day = new Christmas(); © 2004 Pearson Addison-Wesley. All rights reserved

References and Inheritance Assigning a child object to a parent reference is considered to be a widening conversion, and can be performed by simple assignment Assigning a parent object to a child reference can be done also, but it is considered a narrowing conversion and must be done with a cast The widening conversion is the most useful © 2004 Pearson Addison-Wesley. All rights reserved

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 © 2004 Pearson Addison-Wesley. All rights reserved

Example: Animals class hierarchy Animal.java Cow.java Duck.java Dog.java Farm.java © 2004 Pearson Addison-Wesley. All rights reserved

You can use jGrasp to draw diagram like this one © 2004 Pearson Addison-Wesley. All rights reserved

public abstract class Animal { abstract void makeSound(); } public class Cow extends Animal { public void makeSound() { System.out.println("Moo-Moo"); } } public class Dog extends Animal { public void makeSound() { System.out.println(“Wuf-Wuf"); } } public class Duck extends Animal { public void makeSound() { System.out.println(“Quack-Quack"); } } © 2004 Pearson Addison-Wesley. All rights reserved

public class Farm {. public static void main(String[] args). { public class Farm { public static void main(String[] args) { Cow c=new Cow(); Dog d=new Dog(); Duck k= new Duck(); c.makeSound(); d.makeSound(); k.makeSound(); } Result: Moo-Moo Wuf-Wuf Quack-Quack © 2004 Pearson Addison-Wesley. All rights reserved

public class Farm2 {. public static void main(String[] args). { public class Farm2 { public static void main(String[] args) { Animal[] a = new Animal[3]; a[0] = new Cow(); a[1] = new Dog(); a[2] = new Duck(); for(int i=0; i< a.length; i++) a[i].makeSound(); } } Result: Moo-Moo Wuf-Wuf Quack-Quack © 2004 Pearson Addison-Wesley. All rights reserved

public abstract class Animal { abstract void makeSound(); public void move() { System.out.println(“walk”); } } Define a new method which is not abstract and is inherited by all children. public class Cow extends Animal { public void makeSound() { System.out.println("Moo-Moo"); } } public class Dog extends Animal { public void makeSound() { System.out.println(“Wuf-Wuf"); } } public class Duck extends Animal { public void makeSound() { System.out.println(“Quack-Quack"); } } © 2004 Pearson Addison-Wesley. All rights reserved

public abstract class Animal { abstract void makeSound(); public void move() { System.out.println(“walk”); } } public class Cow extends Animal { public void makeSound() { System.out.println("Moo-Moo"); } } public class Dog extends Animal { public void makeSound() { System.out.println(“Wuf-Wuf"); } } public class Duck extends Animal { public void makeSound() { System.out.println(“Quack-Quack"); } public void move() { System.out.println(“fly”); } } Override the move method defined in the Animal class. © 2004 Pearson Addison-Wesley. All rights reserved

Polymorphism via Inheritance Consider the following class hierarchy: StaffMember Executive Hourly Volunteer Employee © 2004 Pearson Addison-Wesley. All rights reserved

Employee Class Hierarchy © 2004 Pearson Addison-Wesley. All rights reserved

Polymorphism via Inheritance Now let's look at an example that pays a set of diverse employees using a polymorphic method See Firm.java (page 486) See Staff.java (page 487) See StaffMember.java (page 489) See Volunteer.java (page 491) See Employee.java (page 492) See Executive.java (page 493) See Hourly.java (page 494) © 2004 Pearson Addison-Wesley. All rights reserved

THE END © 2004 Pearson Addison-Wesley. All rights reserved