Conformance Object-Oriented Programming 236703 Spring 2008 1.

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

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.
Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Class Hierarchy (Inheritance)
Generic programming in Java
5/17/2015 OO Design: Liskov Substitution Principle 1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
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);
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Types in programming languages What are types, and why do we need them? Types in programming languages1.
Comparison of OO Programming Languages © Jason Voegele, 2003.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
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.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
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 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.
Inheritance in the Java programming language J. W. Rider.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
1 COMP313A Programming Languages Object Oriented Progamming Languages (3)
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
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.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Types in programming languages1 What are types, and why do we need them?
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Dynamic Binding Object-Oriented Programming Spring
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Ceg860 (Prasad)L17IT1 Inheritance Techniques Subcontracting Anchored Types.
Abstract Classes and Interfaces Week 17.  Computer simulations  Abstract methods  Abstract classes  Interfaces  Multiple inheritance Abstract Classes.
CSCI-383 Object-Oriented Programming & Design Lecture 24.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Overview of C++ Polymorphism
Variations on Inheritance Object-Oriented Programming Spring
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CSCE 314 Programming Languages Java Generics I Dr. Hyunyoung Lee 1.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
1 clone() Defined in Object Creates an identical copy –Copies pointers to fields (does not copy fields of fields) –Makes a shallow copy if the object’s.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.
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 II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Overview of C++ Polymorphism
Inheritance Lakshmish Ramaswamy.
Presentation transcript:

Conformance Object-Oriented Programming Spring

What’s Conformance? Overriding: replace method body in sub-class. Polymorphism: subclass is usable wherever superclass is usable. Dynamic Biding: consequence of overriding + polymorphism. – Select right method body Conformance: overriding and overridden should be equivalent 2

Conformance and Overriding Ideally, an overriding method should be “semantically compatible” with the overridden one. – All versions of “draw” should draw the object. – Not well-defined. – Impossible to guarantee! We must content ourselves with conformance in the following aspects: – Signature: input arguments, output arguments, input- output arguments, return type, exceptions. – Access. – Contract: pre- and post-conditions, thrown exceptions. 3

“Same or better” Principle Same or better: An overriding method should have at least the same functionality as the overridden method. Overriding should not pose surprises to client. More realistically: All that can be checked is static typing. The “type” of the overriding method should be a super-type of the “type” of the overridden one. The overriding method should have the same or more “calling patterns” as the overridden one. 4

Signature Conformance Elements of signature – Input arguments – Output arguments – Input-Output arguments – Return value – Exceptions Three alternatives: – 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. 5

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 No-variance Contra-variance Co-variance Argument Type Must be C C or base class thereof C or derived class thereof Programming Language Example C++, Java, C# Sather Eiffel 6

Covariance is Natural & Essential More examples: Base: Graph and Node. Derived: MyGraph and MyNode Base: Animal and Food. Derived: Cat and Bones 7

Co-variance is Unsafe class Food{} class Meat extends Food{} class Animal{ void feed(Food f){ //... } class Lion extends Animal{ void feed(Meat m){ //... } class Food{} class Meat extends Food{} class Animal{ void feed(Food f){ //... } class Lion extends Animal{ void feed(Meat m){ //... } void f(Animal a){ Food f = new Food(); a.feed(f); } 8

Co-variance and Static Typing We have seen that co-variance is type unsafe. – It may generate run-time type errors. Q: How can co-variance exist in Eiffel ? – Remember: Eiffel has static type checking A: Eiffel's type system has “holes” – Some messages will not have a compatible method in the receiver – The developer should be aware of that (Similar to programming in a dynamically typed language) – A subclass with a covariant overriding is not a proper subtype of its superclass The compiler will allow assignability But, you may get a “method-not-found” runtime error Possible solution: A whole-world-analysis – The compiler tries to find all possible assignments to a variable => Tries to predict the dynamic type of all variables – Cannot be achieved in the general case 9

Binary Methods A special case of a method with a covariant argument – The argument is of the same type as the receiver – Frequently occurs in many domains: – Comparison & Sorting, Arithmetics, Recursive Datastructures (graphs) class Point { int x, y; boolean same(Point p) { return x == p.x && y == p.y; } { class ColorPoint extends Point { Color color; boolean same(ColorPoint cp) { return x == cp.x && y == cp.y && color == cp.color; } class Point { int x, y; boolean same(Point p) { return x == p.x && y == p.y; } { class ColorPoint extends Point { Color color; boolean same(ColorPoint cp) { return x == cp.x && y == cp.y && color == cp.color; } 10

Binary Methods (cont.) Partial solutions – like current (Eiffel) – Recursive genericity (Java) – Self type (LOOM) if used in a contra-variant position, then no subtyping // Pseudo-code: LOOM class Point { int x, y; boolean same(ThisClass p) { return x == p.x && y == p.y; } } class ColorPoint extends Point { Color color; boolean same(ThisClass cp) { return x == cp.x && y == cp.y && color == cp.color; } Point p = new ColorPoint(); // Error – Not a subtype! // Pseudo-code: LOOM class Point { int x, y; boolean same(ThisClass p) { return x == p.x && y == p.y; } } class ColorPoint extends Point { Color color; boolean same(ThisClass cp) { return x == cp.x && y == cp.y && color == cp.color; } Point p = new ColorPoint(); // Error – Not a subtype! 11

Return Type Suppose that: – m is a method of a base class – m returns a value of class C – m is overridden by m’ in a derived class Q: What is the return type of m’ in the derived class? A: Covariance of return type is both natural and safe 12

Co-variance of Return Type is Safe class Food{} class Meat extends Food{} class Animal{ Food whatDoIEat(){ //... } class Lion extends Animal{ Meat whatDoIEat(){ //... } class Food{} class Meat extends Food{} class Animal{ Food whatDoIEat(){ //... } class Lion extends Animal{ Meat whatDoIEat(){ //... } void f(Animal a){ Food f = a.whatDoIEat(); } 13

Co-variance of Return Type class Employee { public: virtual Employee *clone(void){ return new Employee(*this); } }; class Manager: public Employee { public: virtual Manager *clone(void){ return new Manager(*this); } }; class Employee { public: virtual Employee *clone(void){ return new Employee(*this); } }; class Manager: public Employee { public: virtual Manager *clone(void){ return new Manager(*this); } }; 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 } Using the virtual constructors Define virtual constructors Virtual constructors are an effective technique for implementing “cut” and shape palettes in a GUI draw application. 14

Variance in C++, Java As a rule: No Variance! – Exact match in signature required between: Overriding method Overridden method If a method has a covariant argument... –...It does not override the “old” method – Java: The new method overloads the old one – C++: The new method hides the old one Relaxation: Co-variance is allowed in return value – C++: Must have reference semantics (pointer or reference). – A later addition to both languages – Absolutely type safe – Quite useful 15

Covariance + Overloading = Headache This is a legal Java program – Recall: Covariance of arguments => Overloading class A { boolean f(A a) { return true; } } class B extends A { boolean f(A a) { return false; } boolean f(B b) { return true; } } void f() { A a = new A(); A ab = new B(); B b = new B(); a.f(a); a.f(ab); a.f(b); // true, true, true ab.f(a); ab.f(ab); ab.f(b); // false, false, false b.f(a); b.f(ab); b.f(b); // false, false, true } class A { boolean f(A a) { return true; } } class B extends A { boolean f(A a) { return false; } boolean f(B b) { return true; } } void f() { A a = new A(); A ab = new B(); B b = new B(); a.f(a); a.f(ab); a.f(b); // true, true, true ab.f(a); ab.f(ab); ab.f(b); // false, false, false b.f(a); b.f(ab); b.f(b); // false, false, true } 16

Hiding in C++ A similar program in C++ – Recall: Covariance of arguments => hiding class A { boolean f(A a) { return true; } } class B extends A { boolean f(B b) { return false; } } void f() { A a = new A(); A ab = new B(); B b = new B(); a.f(a); a.f(ab); a.f(b); // true, true, true ab.f(a); ab.f(ab); ab.f(b); // true, true, true b.f(a); b.f(ab); b.f(b); // compilation error! } class A { boolean f(A a) { return true; } } class B extends A { boolean f(B b) { return false; } } void f() { A a = new A(); A ab = new B(); B b = new B(); a.f(a); a.f(ab); a.f(b); // true, true, true ab.f(a); ab.f(ab); ab.f(b); // true, true, true b.f(a); b.f(ab); b.f(b); // compilation error! } 17

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’ 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). Note that adding arguments in an overriding method violates the “same or better” principle! 18

Conformance of Fields Usually, fields can be read-from and written-to – Similar to input-output arguments of a method – Thus, an overriding field should not change the field's type (No-variance) – But, in most languages there's no overriding of fields... What about final fields? – They can only be read from – just like return values – Thus, covariance of final fields is OK 19

Conformance of Exceptions Suppose that: – m is a method of a base class that throws exceptions of types {e 1 …e n } – m is overridden by m’ in a derived class – m ’ throws exceptions of types {x 1 …x m } What is the relationship between {e 1 …e n } and {x 1 …x m }? C++ exceptions are not part of the signature C++ throws clause is checked at run-time Java Recall: a calling function should handle the callee’s exceptions – Catch or declare Therefore, for each x i (1  i  m) we require that there is e j (1  j  n) where x i is a subtype of e j 20

Access Conformance All versions of a method should have the same visibility. – Smalltalk: All methods are public. – Eiffel: Inheritance and export are orthogonal. Sub-class is not strictly a sub-type. – Java: Subclass can only improve the visibility of a method – C++: Not enforced, may lead to breaking of encapsulation. class X{ public: virtual void f(void); }; class Y: public X{ private: void f(void); } y, *py = &y; X *px = py; py->f(); // Error! Y::f is private. px->f(); // OK! (but breaks encapsulation.) class X{ public: virtual void f(void); }; class Y: public X{ private: void f(void); } y, *py = &y; X *px = py; py->f(); // Error! Y::f is private. px->f(); // OK! (but breaks encapsulation.) 21

Friendship and Overriding A friend of base class is not necessarily a friend of the derived class. class X{ friend void amigo(); virtual void f(); }; class Y: public X{ void f(); }; void amigo() { Y* y = new Y(); y->f(); // Error! Y::f is private. X* x = y; // Simple up casting. x->f(); // OK! now the same Y::f is accessible. } class X{ friend void amigo(); virtual void f(); }; class Y: public X{ void f(); }; void amigo() { Y* y = new Y(); y->f(); // Error! Y::f is private. X* x = y; // Simple up casting. x->f(); // OK! now the same Y::f is accessible. } 22

Eiffel: Design by Contract class ACCOUNT feature {NONE} balance: INTEGER count: INTEGER feature {ANY} deposit(sum:INTEGER) is require non_negative: sum >= 0 do balance := balance + sum; count := count + 1; ensure balance >= 0; count = old count + 1; end class ACCOUNT feature {NONE} balance: INTEGER count: INTEGER feature {ANY} deposit(sum:INTEGER) is require non_negative: sum >= 0 do balance := balance + sum; count := count + 1; ensure balance >= 0; count = old count + 1; end 23

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. Rationale: “Same or better principle”. Contracts and Inheritance in Eiffel: – Pre and post conditions are inherited. – They can be refined, but never replaced. Pre-condition refinement: old_pre_condition or new_pre_condition Post-condition refinement: old_post_condition and new_post_condition 24