CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.

Slides:



Advertisements
Similar presentations
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter 10 Classes Continued
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
1 Multi-Methods: The Double-Dispatch Idiom abstract class Ball {... // method, fields for painting/moving a ball static final Color DARK_RED = new Color(150,0,0);
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Overloading, Overriding
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Polymorphism &Virtual Functions
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Effective C#, Chapter 1: C# Language Elements Last Updated: Fall 2011.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
1 COMP313A Programming Languages Object Oriented Progamming Languages (3)
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.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Dynamic Binding Object-Oriented Programming Spring
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Conformance Object-Oriented Programming Spring
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
Ceg860 (Prasad)L17IT1 Inheritance Techniques Subcontracting Anchored Types.
CSCI-383 Object-Oriented Programming & Design Lecture 24.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Overview of C++ Polymorphism
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Essential Ada Terminology copyright © Michael B. Feldman, All Rights Reserved.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
1 Lecture 23: Dynamic Binding (Section ) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
CSCI 383 Object-Oriented Programming & Design Lecture 21 Martin van Bommel.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Inheritance and Run time Polymorphism
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
ATS Application Programming: Java Programming
Virtual Functions Department of CSE, BUET Chapter 10.
Polymorphism Polymorphism
Chapter 8: Class Relationships
Lecture 10 Concepts of Programming Languages
C++ Object Oriented 1.
Presentation transcript:

CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel

Fall 2010CSCI 383 Lecture 22 M. van Bommel 2 Inclusion Polymorphism Inclusion polymorphism: arising from an inclusion relation between sets of values Most inclusion polymorphism is due to subtyping, but not always Examples: Built-in Pascal: the Nil value belongs to all pointer types C: the value 0 belongs to all pointer types C++: the type void* is a subtype of all pointer types User-defined A subclass that is also a subtype

Fall 2010CSCI 383 Lecture 22 M. van Bommel 3 Inclusion Polymorphism Inclusion, or subtype polymorphism arises from inheritance Employee E; Manager M; E.raise_salary(10); // OK M.raise_salary(10); // OK E.is_manager_of(...); // Error M.is_manager_of(...); // OK The code of the raise_salary method is polymorphic It can be applied to all subtypes (subclasses) of Employee We see that without polymorphism, inheritance makes very little sense

Fall 2010CSCI 383 Lecture 22 M. van Bommel 4 Overriding (Inclusion Polymorphism) Like overloading, there are two distinct methods with the same name. But there are differences Overriding only occurs in the context of the parent/child relationship The type signatures must match Overridden methods are sometimes combined together Overriding is usually resolved at run-time, not at compile time

Fall 2010CSCI 383 Lecture 22 M. van Bommel 5 Replacement and Refinement There are actually two different ways that overriding can be handled A replacement totally and completely replaces the code in the parent class with the code in the child class A refinement executes the code in the parent class as part of the code in the child class Most languages use both types of semantics in different situations. Constructors, for example, almost always use refinement

Fall 2010CSCI 383 Lecture 22 M. van Bommel 6 Reasons to Use Replacement There are a number of reasons to use replacement of methods The method in the parent class is abstract, it must be replaced The method in the parent class is a default method and is not appropriate for all situations

Fall 2010CSCI 383 Lecture 22 M. van Bommel 7 Downside of Replacement The downside of replacement semantics is that there is no guarantee that the child class will have any meaning at all similar to the parent class This goes back to the difference between subclasses and subtypes Refinement makes this more difficult to do, since whatever the parent does is guaranteed to be part of the child. This is why most languages use refinement semantics for constructors

Fall 2010CSCI 383 Lecture 22 M. van Bommel 8 Simulate Refinement with Replacement In most languages the most important features of a refinement can be simulated, even if the language uses replacement void Parent::example(int a){ cout << “in parent code\n”; } void Child::example(int a){ Parent::example(12); // do parent code cout << “in child code\n”; // then child code }

Fall 2010CSCI 383 Lecture 22 M. van Bommel 9 Constructors Use Refinement In most languages that have constructors, a constructor will always use refinement This guarantees that whatever initialization the parent class performs will always be included as part of the initialization of the child class

Fall 2010CSCI 383 Lecture 22 M. van Bommel 10 Overriding vs Shadowing vs Redefinition Overriding Type signatures are the same in both parent and child classes Method is declared as virtual in parent Shadowing Type signatures are the same in both parent and child classes Method not declared as virtual in parent Redefinition Type signatures differ in parent and child

Fall 2010CSCI 383 Lecture 22 M. van Bommel 11 Conformance Overriding: replace method body in subclass Polymorphism: subclass is usable wherever superclass is usable Dynamic Binding: consequence of overriding + polymorphism select correct method body Conformance: overriding and overridden should be equivalent Superclass f(arg1, arg2) { … } Subclass f(arg1, arg2) { … } p->f(...)

Fall 2010CSCI 383 Lecture 22 M. van Bommel 12 Conformance and Overriding Thanks to the dynamic binding and polymorphism combination, a client cannot tell which version of a method will be invoked Ideally, an overriding method should be “semantically compatible” with the overridden one E.g., all versions of draw should draw the object Not well-defined Impossible to guarantee! We must content ourselves with conformance in the following aspects Access Contract: pre- and post-conditions, thrown exceptions Signature: input and output arguments, function result

Fall 2010CSCI 383 Lecture 22 M. van Bommel 13 Access Conformance All versions of a method should have the same visibility Smalltalk: All methods are public C++: Not enforced, may lead to breaking of encapsulation class Base{ public: virtual void f(); }; class Derived: public Base{ private: void f(); }; Derived y; Derived* py = &y; Base* px = py; py->f(); // Error! // Derived::f is // private px->f(); // Ok, but breaks // encapsulation

Fall 2010CSCI 383 Lecture 22 M. van Bommel 14 Access Conformance The overriding method should be visible to all components to which the overridden one is visible E.g., overriding enhancing visibility in C++ class Base{ protected: virtual void f(); }; class Derived: public Base{ public: void f(); }; Derived y; Derived* py = &y; Base* px = py; px->f(); // Error! // Base::f is // protected py->f(); // Ok, Derived::f // is public

Fall 2010CSCI 383 Lecture 22 M. van Bommel 15 Friendship and Overriding A friend base class is not a friend of the derived class class Derived; class Base{ friend void amigo(Derived*); protected: virtual void f(); }; class Derived: public Base{ void f(); // Visibility is the same, or is it? }; amigo(Derived* p){ p->f(); // Error! Derived::f is private Base* px = p; // Simple upcasting px->f(); // Ok, now Derived::f is accessible }

Fall 2010CSCI 383 Lecture 22 M. van Bommel 16 Contract Conformance Rules of contract conformance Pre-condition: Overriding method must demand the same or less from its client Post-condition: Overriding method must promise the same or more to its client Exceptions: Overriding method must not throw any exceptions that the overridden doesn’t Contracts and Inheritance in Eiffel Pre- and post-conditions are inherited They can be refined, but never replaced Contracts and Inheritance in C++ The exception-specification ( throw ) list of an overriding function must be at least as restrictive as that of the overridden function

Fall 2010CSCI 383 Lecture 22 M. van Bommel 17 Signature Conformance Elements of signature Input arguments, Output arguments, Input-Output arguments, Result No-variance: The type in signature cannot be changed Co-variance: Change of type in the signature is in the same direction as that of the inheritance Contra-variance: Change of type in the signature is in the opposite direction as that of the inheritance Conformance Contra-variant input arguments Co-variant output arguments No-variant input-output arguments Co-variant return value

Fall 2010CSCI 383 Lecture 22 M. van Bommel 18 Type of Arguments For simplicity, we consider only input arguments Suppose that m is a method of a base class m takes an argument of class C m is overridden by m in a derived class What is the type of the corresponding argument of m in the derived class? Variance Type Argument Type Language Example No-varianceMust be C C++ Contra- variance C or base class thereof Sather Co-variance C or derived class thereof Eiffel

Fall 2010CSCI 383 Lecture 22 M. van Bommel 19 Co-variance is Natural & Essential

Fall 2010CSCI 383 Lecture 22 M. van Bommel 20 Variance in C++ As a rule: No variance! Exact match in signature required between Overriding method Overridden method Relaxation: Co-variance is allowed in return value Must have reference semantics (pointer or reference) Relatively new addition (1992) Absolutely type safe Quite useful

Fall 2010CSCI 383 Lecture 22 M. van Bommel 21 Variance in C++ class Employee{ public: virtual Employee* clone(void){ return new Employee(*this); } }; class Manager: public Employee{ public: virtual Manager* clone(void){ return new Manager(*this); } };

Fall 2010CSCI 383 Lecture 22 M. van Bommel 22 Variance in C++ f(void) { Employee* e1 = new Employee; Employee* e2 = new Manager; Employee* e3; e3 = e1->clone(); // e3 points to an Employee e3 = e2->clone(); // e3 points to a Manager }

Fall 2010CSCI 383 Lecture 22 M. van Bommel 23 Conformance and # Arguments Suppose that m is a method of a base class taking n arguments m is overridden by m in a derived class Then, how many arguments should m in the derived class have? Exactly n: most current programming languages n or less: it does not matter which arguments are omitted as long as naming is consistent n or more: the BETA programming language (daughter of Simula)