© 2004 Pearson Addison-Wesley. All rights reserved April 17, 2006 Polymorphism (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
Advertisements

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 9: Polymorphism Coming up: Binding.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved9-2 Polymorphism Polymorphism (having many forms) is an object- oriented concept.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
1 Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L 9.1 – 9.3.
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.
Chapter Day 22. © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 22 Problem set 4 Posted  10 problems from chapters 7 & 8  Due Nov 21 (right.
ECE122 L22: Polymorphism Using Inheritance April 26, 2007 ECE 122 Engineering Problem Solving with Java Lecture 22 Polymorphism using Inheritance.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 9 Polymorphism 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
Aalborg Media Lab 15-Jul-15 Polymorphism Lecture 12 Chapter 9.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
“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.
Chapter 9 Polymorphism. Chapter Scope The role of polymorphism Dynamic binding Using inheritance for polymorphism Exploring Java interfaces in more detail.
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.
1 Advanced Polymorphism Polymorphism Review Comparator Interface Sorting with Comparators Selection Sort Insertion Sort.
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.
ELC 310 Day 19. © 2004 Pearson Addison-Wesley. All rights reserved9-2 Agenda Questions? Capstone Proposals Overdue  4 Submitted  3 Accepted, 1 in negotiations.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
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:
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.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
© 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.
Modern Programming Tools And Techniques-I
Polymorphism November 27, 2006 ComS 207: Programming I (in Java)
Chapter 9 Polymorphism.
03/10/14 Inheritance-2.
Polymorphism.
Chapter 7: Inheritance Java Software Solutions
Designing for Inheritance
Review for the Final Exam
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.
Overriding Methods & Class Hierarchies
Exceptions April 19, 2006 ComS 207: Programming I (in Java)
Chapter 9 Polymorphism.
Chapter 8 Inheritance Part 2.
Presentation transcript:

© 2004 Pearson Addison-Wesley. All rights reserved April 17, 2006 Polymorphism (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor: Alexander Stoytchev

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

© 2004 Pearson Addison-Wesley. All rights reserved Visibility Cartoon [

© 2004 Pearson Addison-Wesley. All rights reserved [

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

© 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 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 day; day = new Christmas(); Holiday Christmas

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

© 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) { 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) { 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 a

a Not possible since Animal is abstract

© 2004 Pearson Addison-Wesley. All rights reserved But if we add more classes to the Class hierarchy

© 2004 Pearson Addison-Wesley. All rights reserved a We can do this…

© 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"); } } Define a new method called move(). It is not abstract and will be inherited by all children of Animal.

© 2004 Pearson Addison-Wesley. All rights reserved public class Farm2b { 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].move(); } } Result: walk

© 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"); } } Override the move method defined in the Animal class. public class Duck extends Animal { public void makeSound() { System.out.println(“Quack-Quack"); } public void move() { System.out.println(“fly”); } }

© 2004 Pearson Addison-Wesley. All rights reserved public class Farm2c { 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].move(); } } Result: Walk Fly

Chapter 9 Section 9.1 & 9.2

© 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"); } } Only Ducks can dive. public class Duck extends Animal { public void makeSound() { System.out.println(“Quack-Quack"); } public void move() { System.out.println(“fly”); } public void dive() { System.out.println(“Diving…"); } }

© 2004 Pearson Addison-Wesley. All rights reserved public class Farm2d { 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].dive(); } } Compile Error, since dive() is defined only for Duck objects and not for all objects derived from Animal.

© 2004 Pearson Addison-Wesley. All rights reserved public class Farm2d { public static void main(String[] args) { Animal[] a = new Animal[3]; a[0] = new Cow(); a[1] = new Dog(); a[2] = new Duck(); ((Duck)a[2]).dive(); } } This works OK, but requires a cast from a reference to Animal to a reference to Duck.

© 2004 Pearson Addison-Wesley. All rights reserved public class Farm2d { public static void main(String[] args) { Animal[] a = new Animal[3]; a[0] = new Cow(); a[1] = new Dog(); a[2] = new Duck(); ((Duck)a[2]).dive(); } } Result: Diving…

© 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) Firm.java See Staff.java (page 487) Staff.java See StaffMember.java (page 489) StaffMember.java See Volunteer.java (page 491) Volunteer.java See Employee.java (page 492) Employee.java See Executive.java (page 493) Executive.java See Hourly.java (page 494) Hourly.java

© 2004 Pearson Addison-Wesley. All rights reserved Polymorphism via Inheritance Consider the following class hierarchy: StaffMember ExecutiveHourly VolunteerEmployee

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

Chapter 9 Section 9.3

© 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 Polymorphism via Interfaces An interface name can be used as the type of an object reference variable Speaker current; The current reference can be used to point to any object of any class that implements the Speaker interface The version of speak that the following line invokes depends on the type of object that current is referencing current.speak();

© 2004 Pearson Addison-Wesley. All rights reserved Polymorphism via Interfaces Suppose two classes, Philosopher and Dog, both implement the Speaker interface, providing distinct versions of the speak method In the following code, the first call to speak invokes one version and the second invokes another: Speaker guest = new Philospher(); guest.speak(); guest = new Dog(); guest.speak();

© 2004 Pearson Addison-Wesley. All rights reserved The Animals example with interfaces implements In this case Animal is an interface.

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

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

© 2004 Pearson Addison-Wesley. All rights reserved public interface Animal { public void makeSound(); public void move(); } public class Cow implements Animal { public void makeSound() { System.out.println("Moo-Moo"); } public void move() { System.out.println(“walk"); } } public class Dog implements Animal { public void makeSound() { System.out.println(“Wuf-Wuf"); } public void move() { System.out.println(“walk"); } } Define a new method called move(). Because Animal is an interface this method cannot be defined as in the previous example in which Animal was an abstract class. public class Duck implements Animal { public void makeSound() { System.out.println(“Quack-Quack"); } public void move() { System.out.println(“fly"); } } Replicated code!!! The only move() method that is different from the rest.

© 2004 Pearson Addison-Wesley. All rights reserved public class iFarm2 { public static void main(String[] args) { Animal domestic; domestic = new Cow(); domestic.move(); domestic = new Dog(); domestic.move(); domestic = new Duck(); domestic.move(); } Result: walk fly

© 2004 Pearson Addison-Wesley. All rights reserved public interface Animal { public void makeSound(); public void move(); } public class Cow implements Animal { public void makeSound() { System.out.println("Moo-Moo"); } public void move() { System.out.println(“walk"); } } public class Dog implements Animal { public void makeSound() { System.out.println(“Wuf-Wuf"); } public void move() { System.out.println(“walk"); } } public class Duck implements Animal { public void makeSound() { System.out.println(“Quack-Quack"); } public void move() { System.out.println(“fly"); } public void dive() { System.out.println(“Diving…"); } } Only Ducks can dive().

© 2004 Pearson Addison-Wesley. All rights reserved public class iFarm3 { public static void main(String[] args) { Animal domestic; domestic = new Cow(); //domestic.dive(); // error domestic = new Dog(); //domestic.dive(); // error domestic = new Duck(); // domestic.dive(); // error ((Duck)domestic).dive(); // OK, but uses a cast } } Result: Ducks can dive.

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