OOPLs - Inheritance. Inheritance

Similar documents
Inheritance and Substitution (Budd chapter 8, 10)

Data Abstraction. Hwansoo Han

Subtyping (Dynamic Polymorphism)

Modules and Mixins. module Trig PI = def Trig.sin(x) #.. end CSC 517

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Object Oriented Paradigm

Inheritance and Substitution גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Inheritance. Transitivity

CSE Lecture 3: Objects 2 September Nate Nystrom University of Texas at Arlington

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Java Object Oriented Design. CSC207 Fall 2014

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

C++ Yanyan SHEN. slide 1

Chapter 10 Classes Continued. Fundamentals of Java

CS558 Programming Languages

Object Oriented Classes, Objects, Inheritance, and Typing

Objects, Encapsulation, Inheritance (2)

CMSC 132: Object-Oriented Programming II

More on Inheritance. Interfaces & Abstract Classes

Inheritance (Chapter 7)

Inheritance and Substitution עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מובסס על הרצאות של אותו קורס שניתן בשנים הקודמות ע"י ד"ר גרא וייס

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

CMSC 132: Object-Oriented Programming II

History C++ Design Goals. How successful? Significant constraints. Overview of C++

NOTICE WARNING CONCERNING COPYRIGHT RESTRICTIONS: The copyright law of the United States (title 17, U.S. Code) governs the making of photocopies or

CPS 506 Comparative Programming Languages. Programming Language

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Exheritance Class Generalisation Revived

Lecture 16: Object Programming Languages

Type Hierarchy. Lecture 6: OOP, autumn 2003

Inheritance, polymorphism, interfaces

Principles of Programming Languages, 2

Chapter 6 Introduction to Defining Classes

Programming II (CS300)

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

C++ Programming: Introduction to C++ and OOP (Object Oriented Programming)

DATA TYPES. CS 403: Types and Classes DATA TYPES (CONT D)

Object-Oriented Concepts and Design Principles

Concepts of Programming Languages

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Programming Languages 2nd edition Tucker and Noonan"

CS558 Programming Languages Winter 2013 Lecture 8

Software Engineering Fall 2015 (CSC 4350/6350) TR. 5:30 pm 7:15 pm. Rao Casturi 11/03/2015

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Object-Oriented Technology. Rick Mercer

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Programming II (CS300)

Object Orientated Programming Details COMP360

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

Object typing and subtypes

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Module Outline. What is Object-Oriented? Some Possible Definitions. Why Object-oriented? Fundamentals of Object Orientation

Design Pattern and Software Architecture: IV. Design Pattern

Object Oriented Programming

CS 520 Theory and Practice of Software Engineering Fall 2018

Objects, Subclassing, Subtyping, and Inheritance

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

OBJECT ORIENTED PROGRAMMING

What about Object-Oriented Languages?

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007

Chapter 5 Object-Oriented Programming

Object Model Comparisons

Konzepte von Programmiersprachen

COURSE 2 DESIGN PATTERNS

Sorting. Sorting. Selection sort

ob-ject: to feel distaste for something Webster's Dictionary

Lecturer: William W.Y. Hsu. Programming Languages

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Topics in Object-Oriented Design Patterns

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation

L4: Inheritance. Inheritance. Chapter 8 and 10 of Budd.

Goals of design. satisfies problem requirements, usable error free, resistant to incorrect input. readable, not purposefully cryptic.

Inheritance, Polymorphism, and Interfaces

Exercise: Singleton 1

Lecture Notes on Programming Languages

Inheritance (an intuitive description)

Chapter 13. Object Oriented Programming

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Programming Languages & Paradigms PROP HT Abstraction & Modularity. Inheritance vs. delegation, method vs. message. Modularity, cont d.

Parents are Shared Parts of Objects: Inheritance and Encapsulation in SELF *

BASIC CONCEPT OF OOP

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

G Programming Languages - Fall 2012

CS 520 Theory and Practice of Software Engineering Fall 2017

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Interface Class. Lecture 22. Based on Slides of Dr. Norazah Yusof

Common Lisp Object System Specification. 1. Programmer Interface Concepts

CS152: Programming Languages. Lecture 24 Bounded Polymorphism; Classless OOP. Dan Grossman Spring 2011

What is Inheritance?

Static and Dynamic Behavior עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מובסס על הרצאות של אותו קורס שניתן בשנים הקודמות ע "י ד"ר גרא וייס

Transcription:

OOPLs - Inheritance Desirable properties Models of inheritance Class-based: with single, multiple parents Delegation Mix-ins Functionality as code reuse as subtyping 1 Inheritance Data abstraction plus inheritance defines the OO paradigm How to model inheritance to achieve flexibility, ease of code reuse, extensibility (esp. over time) and maintain encapsulation? Example PLs: Simula, Smalltalk-80, C++, Modula-3, Java, 2

Defining Inheritance - Qs Should inheritance be at the level of classes or objects? How should multiple inheritance be defined? Is inheritance subtyping or code reuse? Is-a inheritance versus efficiency in coding How should modification of inherited attributes be constrained? 3 Inheritance- More Qs Concepts and Paradigms of OOP, Peter Wegner, OOPS Messenger, vol 1 no 1 Aug 1990. A mechanism for sharing code and behavior Should we modify inherited attributes? Do we inherit at the level of classes or instances (delegation)? How is multiple inheritance to be defined and managed? What should be inherited? behavior? code? both? 4

Modifiability of Inheritance Behavior compatibility - preserves behavior of parent class B refines A (preserves and augments A s properties) ve.0rsus B is like A Int (1..10) is subtype of Int Signature compatibility - can check usages are syntactically correct E.g., using subtypes as parameters Name compatibility - superclass operation names preserved (possibly refined) in subclass Cancellation - unrestricted modification of superclass by subclass Can cancel superclass attributes 5 Desirable properties A. Snyder, Inheritance and the Development of Encapsulated SW Components, HICSS20, 1987 Should not expose inheritance of members to clients of a class Compromises encapsulation; superclass can t change member definitions easily without affecting subclasses Smalltalk-80 allowed complete access to members by subclasses and users C ++ /Java added protected access control 6

Desirable properties Avoid exposure of class hierarchy itself, so class designer can change hierarchy without users noticing Should not be able to distinguish inherited behaviors from defined ones Should always access ancestor class members through the immediate base class in C ++ need chain of public classes for a user to access members Should be able to exclude base class operations C ++ private inheritance Smalltalk-80 had excludes attribute for subclasses 7 Inheritance Granularity Class-based (ST-80, Java, C++) Delegation - behavior sharing at the level of objects Instances called prototypes serve as templates for behavior sharing and cloning of other instances E.g., SELF PL, David Ungar Can share values or operations Exhibit decrease in stored information at cost of greater complexity in executing operations Comparison: Classes use more storage, less complex operations Delegation uses less storage at cost of more complex operations 8

Delegation Example, Liebermann, OOPSLA 86) Pen Class x,y create() draw() color subclass_of Turtle Class heading backwards() forwards() pen instances <50,200,blue> <100,200,_> CLASSES <50,200,90deg,_> turtle instance pen prototype 50, 200 pen prototype x, y draw() 100 x 90 degrees heading backwards() forwards() delegates to turtle prototype DELEGATION 9 Inheritance Choices in PLs Single (Smalltalk-80) - easier Multiple (C ++, Java) Problem: how to avoid inheriting more than one copy of multiply inherited instance variables or member functions from same ancestor through more than one path? Can linearize hierarchy for lookup purposes (Clos, Flavors) Can exclude some inherited members (CommonObjects, C ++ ) Can define it away at user option (use virtual base class inheritance in C ++ ; use interfaces in Java) 10

How can use inheritance? Many possibilities for why use inheritance Specialization (subtyping, usually assumed in Java, although can have subtyping while redefining implementation: OrderedSets vs. Sets) Specification (parent has virtual or abstract behavior while concrete behavior is defined in child class) Extension - child merely extends parent class behaviors Limitation - child excludes some behavior inherited from parent Combination - multiple inheritance construction - Code sharing but not through an is-a relation (private inheritance in C++, see dequeue example) 11 Inheritance As subtyping Inheriting implementation and external specification S is subtype of T if all operations on type T objects are meaningful on S objects; behavioral substitutability As code reuse Inheriting only implementation; not necessarily an is-a relation Building new components from old 12

Example Two ways to define queue and dequeue Queue append() remove() Dequeue InsertAtFront() RemoveFromRear() subtyping - similar behavior Dequeue InsertAtFront() InsertAtRear() RemoveFrFront() RemoveFrRear() Stack exclude: RemoveFrFront() InsertAtFront() as code reuse Queue exclude: InsertAtFront() RemoveFrRear() 13 Inheritance Multiple versus single Real world is multiple inheritance Linearizing lookup Problem: interpretation depends on non-local inheritance structure, not robust in face of changes No problem if no conflicts fruit apple food pie spice cinnamon Linearized:pie, apple, fruit, cinnamon, spice, food 14

Multiple Inheritance Needed to describe certain complex is-a relationships Endangered species Animal Carnivore Herbivore Cat Bear Cow Leopard Panda Polar 15 Multiple Inheritance Conflict Resolution Actual solutions Disallow multiple inheritance (ST-80) Allow inheritance of indistinguishable components but only one of them (set at defn time) (CLOS, C++) Take approach #2 but pick inherited member at use time (C++, <baseclass>::f()) Combine inherited components into one new component (like flattening the hierarchy) (Flavors) 16

Multiple Inheritance Conflict Resolution Problems: Member clash A f() Inheriting more than one copy of same member Approaches Linearize hierarchy so only one parent is closest (CLOS, Flavors) Throw an exception when same member is applied more than once due to duplicate paths C B f() X g() Y Z W Exclude some members to avoid problem (C++) 17 A. Snyder s Mix-in Classes Use of disjoint parent classes with desired behaviors Reminiscent of Java s interfaces From Stroustrup, The C++ PL virtual class window bordered_window window_w_menu window_w.border+menu 18

Example bordered_window virtual class window _draw() draw() window_w_menu _draw() draw(){window::_draw(); _draw(); } window_w.border+menu draw() calls _draw() and does work for base case _draw() does what s specific to its class. _draw() draw() _draw() draw(){ window::_draw(); bordered_window::_draw(); window_w_menu::_draw(); _draw(); } 19 More on Mixin Inheritance Mixin - an abstract subclass A subclass definition that can be applied to different superclasses to create a related family of modified classes (Bracha-Cook,OOPSLA90) Idea: mixin can be used to specialize the behavior of a variety of parent classes Often by defining methods to perform specific actions and then call the corresponding parent methods 20

cf http://csis.pace.edu/~bergin/patterns/multipleinheritance.html class Parent {public P(int value) {this.val = value;} public int getvalue(){return this.val;} public tostring() {return + this.val;} private int val; } class Other {public Other(int value){..} public void f(){ } } interface OtherInterface { void f();} Java Example class OtherChild extends Other implements OtherInterface {public OtherChild(int value) { super(value);} } class ParentChild extends Parent implements OtherInterface { public ParentChild(..) {child = new OtherChild(..); } public void f(){child.f();} private final OtherInterface child; We have merged the implementations of 2 classes - Parent, Other -- without modifying either one! 21