CMSC 202 Polymorphism.

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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
© 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.
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,
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Programming With Java ICS Chapter 8 Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
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.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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:
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
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.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism and Abstract Classes
Polymorphism November 27, 2006 ComS 207: Programming I (in Java)
CMSC 202 Polymorphism.
Polymorphism 2nd Lecture
Overriding Method.
Polymorphism 2nd Lecture
Polymorphism 2nd Lecture
Comp 249 Programming Methodology
ATS Application Programming: Java Programming
Classes and Objects 2nd Lecture
Object Oriented Programming
Designing for Inheritance
Computer Science II Exam 1 Review.
Inheritance 2nd Lecture
Inheritance Basics Programming with Inheritance
Polymorphism 2nd Lecture
مظفر بگ محمدی دانشگاه ایلام
CMSC 202 Interfaces.
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
Inheritance 2nd Lecture
Computer Programming with JAVA
Java – Inheritance.
Polymorphism CMSC 202, Version 4/02.
Polymorphism Polymorphism - Greek for “many forms”
Inheritance 2nd Lecture
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
CMSC 202 Interfaces.
Polymorphism.
CMSC 202 Generics.
Chapter 9 Carrano Chapter 10 Small Java
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Classes, Objects and Methods
CMSC 202 Inheritance II.
CMSC 202 Interfaces.
Chapter 7 Inheritance.
Presentation transcript:

CMSC 202 Polymorphism

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

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

Animal Hierarchy Animal Dog Cat Pig 10/10

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 { System.out.println( “Pig “ + x ); } 10/10

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/10

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/10

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/10

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/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/10

Late Binding The solution is to use late (dynamic) 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/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. 10/10 Copyright © 2008 Pearson Addison-Wesley. All rights reserved 12

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/10

More Animals Animal Dog Cat Pig Horse Poodle Collie 10/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/10

Copyright © 2008 Pearson Addison-Wesley. 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. 10/10 Copyright © 2008 Pearson Addison-Wesley. All rights reserved 16

Late Binding with toString 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. 10/10 Copyright © 2008 Pearson Addison-Wesley. All rights reserved 17

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. 10/10 Copyright © 2008 Pearson Addison-Wesley. All rights reserved 18

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. 10/10 Copyright © 2008 Pearson Addison-Wesley. All rights reserved 19

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 10/10 Copyright © 2008 Pearson Addison-Wesley. All rights reserved 20