CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.

Slides:



Advertisements
Similar presentations
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
Advertisements

A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
Class Hierarchies. Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 The final Modifier A method marked final indicates that it cannot be overridden with a new definition.
Chapter 8 Polymorphism and Abstract Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
CS102--Object Oriented Programming Lecture 9: – Polymorphism Copyright © 2008 Xiaoyan Li.
Slides prepared by Rose Williams, Binghamton University Chapter 8 Polymorphism Part I.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
CS102--Object Oriented Programming Lecture 10: – Abstract Classes Copyright © 2008 Xiaoyan Li.
© 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.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
CMSC 202 Inheritance II. Version 10/102 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Chapter 8 Polymorphism and Abstract Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Chapter 9: Polymorphism Coming up: Creating Objects Revisited.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Programming With Java ICS Chapter 8 Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
Inheritance Chapter 7. Outline Inheritance Basics Programming with Inheritance Dynamic Binding and Polymorphism.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Peyman Dodangeh Sharif University of Technology Fall 2014.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
EKT472: Object Oriented Programming Overloading and Overriding Method.
Polymorphism and Abstract Classes
CMSC 202 Polymorphism.
Polymorphism 2nd Lecture
Overriding Method.
Polymorphism 2nd Lecture
Polymorphism 2nd Lecture
Comp 249 Programming Methodology
ATS Application Programming: Java Programming
Inheritance 2nd Lecture
Polymorphism 2nd Lecture
مظفر بگ محمدی دانشگاه ایلام
CMSC 202 Interfaces.
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
CMSC 202 Polymorphism.
Polymorphism CT1513.
Polymorphism CMSC 202, Version 4/02.
Inheritance 2nd Lecture
CMSC 202 Interfaces.
Polymorphism.
CMSC 202 Generics.
Chapter 9 Carrano Chapter 10 Small Java
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Chapter 11 Inheritance and Polymorphism Part 2
CMSC 202 Interfaces.
Presentation transcript:

CMSC 202 Polymorphism

10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes

10/20103 Introduction to Polymorphism Object-oriented programming mechanisms  Encapsulation - data and methods together  Inheritance - extending a class for specialization  Polymorphism Polymorphism  The ability to associate many meanings with one method name.  Accomplished through a mechanism known as late binding or dynamic binding. Copyright © 2008 Pearson Addison-Wesley. All rights reserved

10/20104 Animal Hierarchy Animal DogCatPig

10/20105 Animals That Speak public class Animal { public void speak( int x ) { System.out.println(“ Animal “ + x );} } public class Dog extends Animal { public void speak (int x ) { System.out.println( “Dog “ + x ); } } public class Cat extends Animal { public void speak (int x ) { System.out.println( “Cat “ + x ); } } public class Pig extends Animal { public void speak (int x ) { System.out.println( “Pig “ + x ); } }

10/20106 The ZooDemo Class In the ZooDemo, we ask each Animal to say hello to the audience. public class ZooDemo { // Overloaded type-specific sayHello method // for each kind of Animal public static void sayHello( Dog d, int i ) { d.speak( i ); } public static void sayHello( Cat c, int i ) { c.speak( i ); } public static void sayHello( Pig p, int i) { p.speak( i ); } (continued)

10/20107 The ZooDemo Class public static void main( String[ ] args ) { Dog dusty = new Dog( ); Cat fluffy = new Cat( ); Pig sam = new Pig( ); sayHello( dusty, 7 ); sayHello( fluffy, 17 ); sayHello( sam, 27 ); } } // end Zoo Demo // output Dog 7 Cat 17 Pig 27

10/20108 Problems with ZooDemo? The ZooDemo class contains a type-specific version of sayHello for each type of Animal. What if we add more types of Animals? Wouldn’t it be nice to write just one sayHello method that works for all animals?

10/20109 New ZooDemo public class ZooDemo { // One sayHello method whose parameter // is the base class works for all Animals public static void sayHello( Animal a, int x ) { a.speak( x ); } public static void main( String[ ] args ) { Dog dusty = new Dog( ); Cat fluffy = new Cat( ); Pig sam = new Pig( ); sayHello( dusty, 7 ); sayHello( fluffy, 17 ); sayHello( sam, 27 ); } }

10/ How Does New ZooDemo work? Associating the appropriate method definition with the method invocation is known as binding. Early binding occurs when the method definition is associated with its invocation when code is compiled.  With early binding, the method invoked is determined by the reference variable type. How can the compiler know which Animal’s speak method to call in sayHello? It can’t!

10/ Late Binding The solution is to use late (dynamic) binding. Late binding  The appropriate method definition is associated with its invocation at run-time.  The method invoked is determined by the type of object to which the variable refers, NOT by the type of the reference variable. Java uses late binding for all methods except  final,  private (which are implicitly final), and  static methods.

10/ An Object Knows the Definitions of Its Methods The type of a class variable determines which method names can be used with the variable.  However, the object named by the variable determines which definition with the same method name is used. A special case of this rule:  The type of a class parameter determines which method names can be used with the parameter.  The argument determines which definition of the method name is used. Copyright © 2008 Pearson Addison-Wesley. All rights reserved

10/ Using Polymorphism How do we take advantage of Polymorphism?  Write code to talk to base class objects (e.g. use base class references as method parameters).  Late binding will ensure that the appropriate method definition is used, even if a reference to a derived class is passed to the method.

10/ More Animals Animal DogCatPigHorse PoodleCollie

10/ Extensibility Suppose more Animals were added to the hierarchy as shown in the previous diagram. All of these new classes work correctly with the old, unchanged sayHello method of the ZooDemo because sayHello’s parameter is a base class reference. In a well designed OOP program, most of your methods will follow the model of sayHello and communicate with a base class reference and let late binding and polymorphism determine which sayHello method to call. Such a program is called extensible because you can add new functionality by deriving new classes from the base class without changing existing code.

10/ The final Modifier A method marked final indicates that it cannot be overridden with a new definition in a derived class.  If final, the compiler can use early binding with the method. public final void someMethod() {... } A class marked final indicates that it cannot be used as a base class from which to derive any other classes. Copyright © 2008 Pearson Addison-Wesley. All rights reserved

10/ If an appropriate toString method is defined for a class, then an object of that class can be output using System.out.println( ); As in this code snippet Animal max = new Animal( ); System.out.println(max); This works because of late binding. Late Binding with toString Copyright © 2008 Pearson Addison-Wesley. All rights reserved

10/ Late Binding with toString One definition of the method println takes a single argument of type Object : public void println(Object theObject) { System.out.println(theObject.toString()); }  In turn, It invokes the version of println that takes a String argument. Note that the println method was defined before the Animal class existed. Because of late binding, the toString method from the Animal class is used, not the toString from the Object class. Copyright © 2008 Pearson Addison-Wesley. All rights reserved

10/ Upcasting and Downcasting Upcasting occurs when an object of a derived class is assigned to a variable of a base class (or any ancestor class). Animal animalVariable; // base class Dog dogVariable = new Dog( );// derived class animalVariable = dogVariable; // upcasting animalVariable.speak(42);// “Dog 42” is printed Or we could do something equivalent, such as Animal animal = new Dog( ); Because of late binding, speak uses the definition of speak given in the Dog class. Copyright © 2008 Pearson Addison-Wesley. All rights reserved

10/ Upcasting and Downcasting Downcasting occurs when a type cast is performed from a base class to a derived class (or from any ancestor class to any descendent class).  Downcasting must be done very carefully.  In many cases it doesn't make sense, or is illegal: dogVariable = //will produce (Dog)animalVariable;//run-time error dogVariable = animalVariable //will produce //compiler error  There are times when downcasting is necessary; e.g., inside the equals method for a class: Dog otherDog = (Dog)otherDog;//downcasting Copyright © 2008 Pearson Addison-Wesley. All rights reserved