Polymorphism 2019/4/29.

Slides:



Advertisements
Similar presentations
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Advertisements

Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
© 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.
Polymorphism &Virtual Functions
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Lecture 9 Polymorphism Richard Gesick.
JAVA Introduction ● One of the main JAVA design goal is reducing complexity for programmer – Development time is half or less comparing to equivalent C++
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Reusing Classes composition The trick is to use the classes without soiling the existing code. In this chapter you’ll see two ways to accomplish.
Object-Oriented Programming Chapter Chapter
M1G Introduction to Programming 2 5. Completing the program.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
ISBN Object-Oriented Programming Chapter Chapter
CSE 143 Lecture 12 Inheritance slides created by Ethan Apter
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
CSE 143 Lecture 13 Inheritance slides created by Ethan Apter
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Design issues for Object-Oriented Languages
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
7: Polymorphism Upcasting revisited Forgetting the object type
Modern Programming Tools And Techniques-I
Virtual Function and Polymorphism
Polymorphism, Virtual Methods and Abstract Classes
Chapter 11 Inheritance and Polymorphism
Inheritance Based on slides by Ethan Apter
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Final and Abstract Classes
Inheritance and Polymorphism
Polymorphism &Virtual Functions
Object-Oriented Programming
Polymorphism.
Inheritance and Run time Polymorphism
Inheritance in Java.
JAVA Introduction ការណែនាំពី Java
Polymorphism & Virtual Functions
Comp 249 Programming Methodology
Lecture 23 Polymorphism Richard Gesick.
Chapter 9 Inheritance and Polymorphism
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
Virtual Functions Department of CSE, BUET Chapter 10.
Advanced Java Topics Chapter 9
Packages and Interfaces
Week 6 Object-Oriented Programming (2): Polymorphism
CMSC 202 Polymorphism.
Polymorphism Polymorphism
Java – Inheritance.
Polymorphism CMSC 202, Version 4/02.
Fundaments of Game Design
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Final and Abstract Classes
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
C++ Object Oriented 1.
Lecture 6: Polymorphism
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

Polymorphism 2019/4/29

Upcasting revisited Taking an object reference and treating it as a reference to its base type is called upcasting because of the way inheritance trees are drawn with the base class at the top. 2019/4/29

Forgetting the object type Why should anyone intentionally forget the type of an object? This is what happens when you upcast. It is nice if you could forget that there are derived classes, and write your code to talk only to the base class. 2019/4/29

Method-call binding Connecting a method call to a method body is called binding. When binding is performed before the program is run (by the compiler and linker, if there is one), it’s called early binding. And the late binding means that the binding occurs at run time, based on the type of object. Late binding is also called dynamic binding or runtime binding. 2019/4/29

All method binding in Java uses late binding unless the method is static or final (private methods are implicitly final). This means that ordinarily you don’t need to make any decisions about whether late binding will occur—it happens automatically. 2019/4/29

Why would you declare a method final Why would you declare a method final? As noted in the last chapter, it prevents anyone from overriding that method. Perhaps more important, it effectively “turns off” dynamic binding, or rather it tells the compiler that dynamic binding isn’t necessary. This allows the compiler to generate slightly more efficient code for final method calls. However, in most cases it won’t make any overall performance difference in your program, so it’s best to only use final as a design decision, and not as an attempt to improve performance. 2019/4/29

Producing the right behavior “send a message to an object and let the object figure out the right thing to do.” you can write your code to talk to the base class and know that all the derived-class cases will work correctly using the same code. 2019/4/29

Classic Shape Example The shape example has a base class called Shape and various derived types: Circle, Square, Triangle, etc. 2019/4/29

Extensibility Because of polymorphism, you can add as many new types as you want to the system without changing the method. In a well-designed OOP program, most or all of your methods will follow the model of this method and communicate only with the base-class interface. 2019/4/29

Such a program is extensible because you can add new functionality by inheriting new data types from the common base class. The methods that manipulate the base-class interface will not need to be changed at all to accommodate the new classes. 2019/4/29

Example 2019/4/29

Pitfall: “overriding” private methods Only non-private methods may be overridden, but you should watch out for the appearance of overriding private methods, which generates no compiler warnings, but doesn’t do what you might expect. To be clear, you should use a different name from a private base-class method in your derived class. 2019/4/29

Pitfall: fields and static methods For one thing, you’ll generally make all fields private and so you won’t access them directly, but only as side effects of calling methods. In addition, you probably won’t give the same name to a base-class field and a derived-class field, because its confusing. If a method is static, it doesn’t behave polymorphically. 2019/4/29

Constructors and polymorphism Constructors are different from other kinds of methods. This is also true when polymorphism is involved. Even though constructors are not polymorphic (they’re actually static methods, but the static declaration is implicit), it’s important to understand the way constructors work in complex hierarchies and with polymorphism 2019/4/29

Order of constructor calls A constructor for the base class is always called during the construction process for a derived class, chaining up the inheritance hierarchy so that a constructor for every base class is called. This makes sense because the constructor has a special job: to see that the object is built properly. 2019/4/29

A derived class has access to its own members only, and not to those of the base class (whose members are typically private). Only the base-class constructor has the proper knowledge and access to initialize its own elements. Therefore, it’s essential that all constructors get called; otherwise the entire object wouldn’t be constructed. 2019/4/29

If there is no default constructor, the compiler will complain. It will silently call the default constructor if you don’t explicitly call a base-class constructor in the derived-class constructor body. If there is no default constructor, the compiler will complain. 2019/4/29

When you inherit, you know all about the base class and can access any public and protected members of the base class. You must be able to assume that all the members of the base class are valid when you’re in the derived class. The only way to guarantee this is for the base-class constructor to be called first. Then when you’re in the derived-class constructor, all the members you can access in the base class have been initialized 2019/4/29

Inheritance and cleanup If you do have cleanup issues, you must be diligent and create a dispose( ) method (the name I have chosen to use here; you may come up with something better) for your new class. And with inheritance, you must override dispose( ) in the derived class if you have any special cleanup that must happen as part of garbage collection. 2019/4/29

When you override dispose( ) in an inherited class, it’s important to remember to call the base-class version of dispose( ), since otherwise the base-class cleanup will not happen. 2019/4/29

Behavior of polymorphic methods inside constructors If you call a dynamically-bound method inside a constructor, the overridden definition for that method is used. However, the effect of this call can be rather unexpected because the overridden method will be called before the object is fully constructed. This can conceal some difficult-to-find bugs 2019/4/29

Actual process of initialization 1. The storage allocated for the object is initialized to binary zero before anything else happens. 2. The base-class constructors are called as described previously. At this point, the overridden draw( ) method is called (yes, before the RoundGlyph constructor is called), which discovers a radius value of zero, due to Step 1. 3. Member initializers are called in the order of declaration. 4. The body of the derived-class constructor is called. 2019/4/29

Covariant return types Java SE5 adds covariant return types, which means that an overridden method in a derived class can return a type derived from the type returned by the base-class method. 2019/4/29

Designing with inheritance Once you learn about polymorphism, it can seem that everything ought to be inherited, because polymorphism is such a clever tool. This can burden your designs. A better approach is to choose composition first, especially when it’s not obvious which one you should use. 2019/4/29

Substitution vs. extension t would seem that the cleanest way to create an inheritance hierarchy is to take the “pure” approach. That is, only methods that have been established in the base class are overridden in the derived class. 2019/4/29

Pure substitution 2019/4/29

This can be thought of as pure substitution This can be called a pure “is-a” relationship because the interface of a class establishes what it is. Inheritance guarantees that any derived class will have the interface of the base class and nothing less. If you follow this diagram, derived classes will also have no more than the base-class interface. This can be thought of as pure substitution 2019/4/29

That is, the base class can receive any message you can send to the derived class because the two have exactly the same interface. 2019/4/29

Extension 2019/4/29

This can be termed an “is-like-a” relationship, because the derived class is like the base class—it has the same fundamental interface—but it has other features that require additional methods to implement 2019/4/29

Downcasting and runtime type information to move back down the inheritance hierarchy—you use a downcast. There must be some way to guarantee that a downcast is correct, so that you won’t accidentally cast to the wrong type and then send a message that the object can’t accept. This would be quite unsafe 2019/4/29

in Java, every cast is checked in Java, every cast is checked. You get a ClassCastException if error occurred. This act of checking types at run time is called runtime type identification (RTTI). 2019/4/29

Summary Polymorphism means “different forms.” In object-oriented programming, you have the same interface from the base class, and different forms using that interface: the different versions of the dynamically bound methods. To use polymorphism—and thus object-oriented techniques—effectively in your programs, you must expand your view of programming to include not just members and messages of an individual class, but also the commonality among classes and their relationships with each other. 2019/4/29