Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,

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

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.
Generics and the ArrayList Class
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li.
© 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.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
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 8: – More about Inheritance When to use inheritance Relationship between classes Rules to follow Copyright ©
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
CS102--Object Oriented Programming Lecture 9: – Polymorphism Copyright © 2008 Xiaoyan Li.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Chapter 7 Inheritance Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 8 Polymorphism Part I.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
CS102--Object Oriented Programming Lecture 10: – Abstract Classes Copyright © 2008 Xiaoyan Li.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
CMSC 202 Inheritance II. Version 10/102 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
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.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
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.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
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.
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.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Comp 248 Introduction to Programming Chapter 6 Arrays Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
Polymorphism and Abstract Classes
Polymorphism 2nd Lecture
Inheritance and Polymorphism
Polymorphism 2nd Lecture
Polymorphism 2nd Lecture
Comp 249 Programming Methodology
Comp 249 Programming Methodology
Inheritance 2nd Lecture
Inheritance Basics Programming with Inheritance
Polymorphism 2nd Lecture
Comp 249 Programming Methodology
CMSC 202 Polymorphism.
Comp 249 Programming Methodology
Inheritance 2nd Lecture
Computer Programming with JAVA
Inheritance 2nd Lecture
Polymorphism.
Comp 249 Programming Methodology
Classes, Objects and Methods
CMSC 202 Inheritance II.
Chapter 8 Abstract Classes.
Chapter 7 Inheritance.
Presentation transcript:

Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been extracted, modified and updated from original slides of Absolute Java 3 rd Edition by Savitch; which has originally been prepared by Rose Williams of Binghamton University. Absolute Java is published by Pearson Education / Addison-Wesley. Copyright © 2007 Pearson Addison-Wesley Copyright © 2013 Aiman Hanna All rights reserved

Introduction to Polymorphism There are three main programming mechanisms that constitute object-oriented programming (OOP) There are three main programming mechanisms that constitute object-oriented programming (OOP) Encapsulation Encapsulation Inheritance Inheritance Polymorphism Polymorphism Polymorphism is the ability to associate many meanings to one method name Polymorphism is the ability to associate many meanings to one method name It does this through a special mechanism known as late binding or dynamic binding It does this through a special mechanism known as late binding or dynamic binding 8-2

Introduction to Polymorphism Inheritance allows a base class to be defined, and other classes derived from it Inheritance allows a base class to be defined, and other classes derived from it Code for the base class can then be used for its own objects, as well as objects of any derived classes Code for the base class can then be used for its own objects, as well as objects of any derived classes Polymorphism allows changes to be made to method definitions in the derived classes, and have those changes apply to the software written for the base class Polymorphism allows changes to be made to method definitions in the derived classes, and have those changes apply to the software written for the base class 8-3

Late Binding The process of associating a method definition with a method invocation is called binding The process of associating a method definition with a method invocation is called binding If the method definition is associated with its invocation when the code is compiled, that is called early binding If the method definition is associated with its invocation when the code is compiled, that is called early binding If the method definition is associated with its invocation when the method is invoked (at run time), that is called late binding or dynamic binding If the method definition is associated with its invocation when the method is invoked (at run time), that is called late binding or dynamic binding 8-4

Late Binding Java uses late binding for all methods (except private, final, and static methods) Java uses late binding for all methods (except private, final, and static methods) Because of late binding, a method can be written in a base class to perform a task, even if portions of that task aren't yet defined Because of late binding, a method can be written in a base class to perform a task, even if portions of that task aren't yet defined See Polymorphism1.java 8-5

Pitfall: No Late Binding for Static Methods When the decision of which definition of a method to use is made at compile time, that is called static binding When the decision of which definition of a method to use is made at compile time, that is called static binding This decision is made based on the type of the variable naming the object This decision is made based on the type of the variable naming the object Java uses static, not late, binding with private, final, and static methods Java uses static, not late, binding with private, final, and static methods In the case of private and final methods, late binding would serve no purpose (these methods cannot be overridden, so only one version exists) In the case of private and final methods, late binding would serve no purpose (these methods cannot be overridden, so only one version exists) (Warning:) However, in the case of a static method invoked using a calling object, it does make a difference (Warning:) However, in the case of a static method invoked using a calling object, it does make a difference See Polymorphism2.java 8-6

Pitfall: No Late Binding for Static Methods Example (See Polymorphism2.java): The Vehicle class DisplayNumberOfCreatedObjects () method: The Vehicle class DisplayNumberOfCreatedObjects () method: public static void DisplayNumberOfCreatedObjects() { System.out.println("The number of created Vehicle objects so far is " + numOfCreatedObjects + "."); } The Bus class DisplayNumberOfCreatedObjects () method: The Bus class DisplayNumberOfCreatedObjects () method: public static void DisplayNumberOfCreatedObjects() { System.out.println("The number of created Bus objects so far is " + numOfCreatedObjects + "."); } 8-7

Pitfall: No Late Binding for Static Methods Example (See Polymorphism2.java) – Continues: In the previous example, the object v1 was created from the Vehicle class, and the object b1 was created from the Bus class, In the previous example, the object v1 was created from the Vehicle class, and the object b1 was created from the Bus class, Given the following assignment: Given the following assignment: v1 = b1; Now the two variables point to the same object Now the two variables point to the same object 8-8

Pitfall: No Late Binding for Static Methods Example (See Polymorphism2.java) – Continues: Given the invocations: Given the invocations: v1.DisplayNumberOfCreatedObjects(); b1.DisplayNumberOfCreatedObjects(); The output is: The number of created Vehicle objects so far is 11. The number of created Bus objects so far is 3. Note that here, DisplayNumberOfCreatedObjects is a static method invoked a calling object (i.e. v1, b1) (instead of its class name) Note that here, DisplayNumberOfCreatedObjects is a static method invoked a calling object (i.e. v1, b1) (instead of its class name) Therefore the exact executed method is determined by its variable name, not the object that it references Therefore the exact executed method is determined by its variable name, not the object that it references 8-9

Pitfall: No Late Binding for Static Methods There are other cases where a static method has a calling object in a more inconspicuous way There are other cases where a static method has a calling object in a more inconspicuous way For example, a static method can be invoked within the definition of a nonstatic method, but without any explicit class name or calling object For example, a static method can be invoked within the definition of a nonstatic method, but without any explicit class name or calling object In this case, the calling object is the implicit this In this case, the calling object is the implicit this 8-10

The final Modifier A method marked final indicates that it cannot be overridden with a new definition in a derived class 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 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 A class marked final indicates that it cannot be used as a base class from which to derive any other classes 8-11

Upcasting and Downcasting Upcasting is when an object of a derived class is assigned to a variable of a base class (or any ancestor class) Upcasting is when an object of a derived class is assigned to a variable of a base class (or any ancestor class) Vehicle v1 = new Vehicle(); //Base class object Bus b1 = new Bus(2, 55000, 37); //Derived class object v1 = b1; Downcasting is when a type cast is performed from a base class to a derived class (or from any ancestor class to any descendent class) Downcasting is when a type cast is performed from a base class to a derived class (or from any ancestor class to any descendent class) Downcasting has to be done very carefully Downcasting has to be done very carefully In many cases it doesn't make sense, or is illegal: In many cases it doesn't make sense, or is illegal: B1 = v1; //will produce compiler error B1 = (Bus)v1; //will produce run-time error There are times, however, when downcasting is necessary, e.g., inside the equals method for a class There are times, however, when downcasting is necessary, e.g., inside the equals method for a class See Polymorphism3.java Revisit Object3.java 8-12

Pitfall: Downcasting It is the responsibility of the programmer to use downcasting only in situations where it makes sense It is the responsibility of the programmer to use downcasting only in situations where it makes sense The compiler does not check to see if downcasting is a reasonable thing to do The compiler does not check to see if downcasting is a reasonable thing to do Using downcasting in a situation that does not make sense usually results in a run-time error Using downcasting in a situation that does not make sense usually results in a run-time error 8-13

Tip: Checking to See if Downcasting is Legitimate Downcasting to a specific type is only sensible if the object being cast is an instance of that type Downcasting to a specific type is only sensible if the object being cast is an instance of that type This is exactly what the instanceof operator tests for: This is exactly what the instanceof operator tests for: object instanceof ClassName It will return true if object is of type ClassName ; in particular, it will return true if object is an instance of any descendent class of ClassName It will return true if object is of type ClassName ; in particular, it will return true if object is an instance of any descendent class of ClassName 8-14

Pitfall: Limitations of Copy Constructors A copy constructor is supposed to create a good copy of a new object from an existing one A copy constructor is supposed to create a good copy of a new object from an existing one However, when polymorphism is used, a copy constructor may have a strong limitation However, when polymorphism is used, a copy constructor may have a strong limitation See Polymorphism4.java 8-15

A First Look at the clone Method Every object inherits a method named clone from the Object class Every object inherits a method named clone from the Object class The method clone has no parameters The method clone has no parameters It is supposed to return a deep copy of the calling object It is supposed to return a deep copy of the calling object However, the inherited version of the method is not designed to be used as is However, the inherited version of the method is not designed to be used as is Instead, each class is expected to override it with a more appropriate version Instead, each class is expected to override it with a more appropriate version 8-16

A First Look at the clone Method The heading for the clone method defined in the Object class is as follows: The heading for the clone method defined in the Object class is as follows: protected Object clone() The heading for a clone method that overrides the clone method in the Object class can differ somewhat from the heading above The heading for a clone method that overrides the clone method in the Object class can differ somewhat from the heading above A change to a more permissive access, such as from protected to public, is always allowed when overriding a method definition A change to a more permissive access, such as from protected to public, is always allowed when overriding a method definition Changing the return type from Object to the type of the class being cloned is allowed because every class is a descendent class of the class Object Changing the return type from Object to the type of the class being cloned is allowed because every class is a descendent class of the class Object This is an example of a covariant return type This is an example of a covariant return type 8-17

A First Look at the clone Method If a class has a copy constructor, the clone method for that class can use the copy constructor to create the copy returned by the clone method If a class has a copy constructor, the clone method for that class can use the copy constructor to create the copy returned by the clone method public Vehicle clone() { return new Sale(this); return new Sale(this);} and another example: and another example: public Bus clone() { return new Bus(this); return new Bus(this);} See Polymorphism5.java 8-18

Pitfall: Limitations of Copy Constructors Although the clone() methods may in fact use the copy constructors to perform the copying, this works because the method clone has the same name in all classes, and polymorphism works with method names Although the clone() methods may in fact use the copy constructors to perform the copying, this works because the method clone has the same name in all classes, and polymorphism works with method names The copy constructors (i.e Vehicle, Bus, RaceCar ) have different names, and polymorphism doesn't work with methods of different names The copy constructors (i.e Vehicle, Bus, RaceCar ) have different names, and polymorphism doesn't work with methods of different names 8-19

Pitfall: Sometime the clone Method Return Type is Object Prior to version 5.0, Java did not allow covariant return types, so no changes whatsoever were allowed in the return type of an overridden method Prior to version 5.0, Java did not allow covariant return types, so no changes whatsoever were allowed in the return type of an overridden method Therefore, the clone method for all classes had Object as its return type Therefore, the clone method for all classes had Object as its return type Consequently, the clone method for any class, i.e. the Vehicle class would have looked like this: Consequently, the clone method for any class, i.e. the Vehicle class would have looked like this: public Object clone() { return new Vehicle(this); return new Vehicle(this);} Therefore, the result needed to always be type casted when using a clone method written for an older version of Java Therefore, the result needed to always be type casted when using a clone method written for an older version of Java Vehicle newVec = (Vehicle)originalVec.clone(); 8-20

Pitfall: Sometime the clone Method Return Type is Object It is still perfectly legal to use Object as the return type for a clone method, even with classes defined after Java version 5.0 It is still perfectly legal to use Object as the return type for a clone method, even with classes defined after Java version 5.0 When in doubt, it causes no harm to include the type cast When in doubt, it causes no harm to include the type cast For example, the following is legal for the clone method of the Vehicle class: For example, the following is legal for the clone method of the Vehicle class: Vehicle newVec = originalVec.clone(); However, adding the following type cast produces no problems: However, adding the following type cast produces no problems: Vehicle newVec = (Vehicle)originalVec.clone(); 8-21

Abstract Class Sometimes, it is does NOT make sense to create objects from specific classes Sometimes, it is does NOT make sense to create objects from specific classes In such case, these classes should be created as abstract In such case, these classes should be created as abstract An abstract class can only be used to derive other classes; you cannot create objects from an abstract class An abstract class can only be used to derive other classes; you cannot create objects from an abstract class An abstract class must have at least one abstract method An abstract class must have at least one abstract method 8-22

Abstract Method An abstract method has a complete method heading, to which has been added the modifier abstract An abstract method has a complete method heading, to which has been added the modifier abstract It has no method body, and ends with a semicolon in place of its body It has no method body, and ends with a semicolon in place of its body public abstract long getSerNumber(); An abstract method cannot be private An abstract method cannot be private See Abstract1.java 8-23

Abstract Class An abstract class can have any number of abstract and/or fully defined methods An abstract class can have any number of abstract and/or fully defined methods If a derived class of an abstract class adds to or does not define all of the abstract methods, then it is abstract also, and must add abstract to its modifier If a derived class of an abstract class adds to or does not define all of the abstract methods, then it is abstract also, and must add abstract to its modifier A class that has no abstract methods is called a concrete class A class that has no abstract methods is called a concrete class 8-24

Pitfall: You Cannot Create Instances of an Abstract Class An abstract class constructor cannot be used to create an object of the abstract class An abstract class constructor cannot be used to create an object of the abstract class However, a derived class constructor will include an invocation of the abstract class constructor in the form of super However, a derived class constructor will include an invocation of the abstract class constructor in the form of super Although an object of an abstract class cannot be created, it is perfectly fine to have a parameter of an abstract class type Although an object of an abstract class cannot be created, it is perfectly fine to have a parameter of an abstract class type This makes it possible to plug in an object of any of its descendent classes This makes it possible to plug in an object of any of its descendent classes 8-25