Francesco Nidito. Programmazione Avanzata AA 2007/08

Similar documents
Francesco Nidito. Programmazione Avanzata AA 2007/08

Inheritance, Polymorphism and the Object Memory Model

Francesco Nidito. Programmazione Avanzata AA 2007/08

Object typing and subtypes

What about Object-Oriented Languages?

Data Abstraction. Hwansoo Han

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

Resource Management With a Unique Pointer. Resource Management. Implementing a Unique Pointer Class. Copying and Moving Unique Pointers

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Inheritance and object compatibility

Concepts of Programming Languages

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University

Java Fundamentals (II)

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

Object Model. Object Oriented Programming Spring 2015

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

First IS-A Relationship: Inheritance

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

More Relationships Between Classes

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

Java Object Oriented Design. CSC207 Fall 2014

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

CS558 Programming Languages Winter 2013 Lecture 8

C++ Important Questions with Answers

Week 5-1: ADT Design

Inheritance, polymorphism, interfaces

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

CS105 C++ Lecture 7. More on Classes, Inheritance

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

COMP322 - Introduction to C++ Lecture 09 - Inheritance continued

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

Comp 311 Principles of Programming Languages Lecture 21 Semantics of OO Languages. Corky Cartwright Mathias Ricken October 20, 2010

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

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

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

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

1.00 Lecture 13. Inheritance

Polymorphism Part 1 1

Inheritance, Polymorphism, and Interfaces

C++ Inheritance and Encapsulation

The Procedure Abstraction

CPS 506 Comparative Programming Languages. Programming Language

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

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

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

OBJECT ORİENTATİON ENCAPSULATİON

Inheritance -- Introduction

Implements vs. Extends When Defining a Class

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

Subtyping (Dynamic Polymorphism)

The Java Programming Language

Chapter 5 Object-Oriented Programming

Strict Inheritance. Object-Oriented Programming Spring 2008

CSE 401/M501 Compilers

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

Chapter 10 :: Data Abstraction and Object Orientation

Overriding Variables: Shadowing

Object-Oriented Programming, Iouliia Skliarova

CS-202 Introduction to Object Oriented Programming

Object Model. Object Oriented Programming Winter

Instantiation of Template class

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

CS 251 Intermediate Programming Inheritance

Programming in C# Inheritance and Polymorphism

Strict Inheritance. Object-Oriented Programming Winter

Programming Languages 2nd edition Tucker and Noonan"

Discussion. Type 08/12/2016. Language and Type. Type Checking Subtypes Type and Polymorphism Inheritance and Polymorphism

Inheritance & Polymorphism. Object-Oriented Programming Spring 2015

Object Oriented Programming: Based on slides from Skrien Chapter 2

Comp 249 Programming Methodology Chapter 8 - Polymorphism

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Practice for Chapter 11

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

2. The object-oriented paradigm!

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT

Type Hierarchy. Lecture 6: OOP, autumn 2003

CSE 431S Type Checking. Washington University Spring 2013

Procedure and Object- Oriented Abstraction

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

CS558 Programming Languages

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

2. The object-oriented paradigm

Semantic Analysis. Lecture 9. February 7, 2018

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

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

Information System Design (IT60105)

Object Orientation. Chapter Sixteen Modern Programming Languages, 2nd ed. 1

What are the characteristics of Object Oriented programming language?

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

CMSC 132: Object-Oriented Programming II

CSc 520. Principles of Programming Languages 45: OO Languages Introduction

Transcription:

Francesco Nidito Programmazione Avanzata AA 2007/08

Outline 1 2 3 4 Reference: Micheal L. Scott, Programming Languages Pragmatics, Chapter 10

, type systems and type checking A type type is an abstraction system that defines the memory layout of data a set of operations that can be performed on value belonging to that type

, type systems and type checking A type type is an abstraction system that defines the memory layout of data a set of operations that can be performed on value belonging to that type A type system consists of a mechanism for defining types and associating them to language structures a set of rules for: type equivalence, type compatibility and type inference

, type systems and type checking A type type is an abstraction system that defines the memory layout of data a set of operations that can be performed on value belonging to that type A type system consists of a mechanism for defining types and associating them to language structures a set of rules for: type equivalence, type compatibility and type inference Type checking is the process of ensuring that a program obeys the language s type compatibility rules

Class type Class is a type constructor like struct or array A class combines Data (like structs) Methods (operations on the data) A class has two special operations to provide Initialization Finalization

Class B inherits from class A (B <: A) when an object of class A is expected an object of class B can be used instead (Liskow substitution) Student <: Person - a student can do everything a person can do expresses the idea of adding features to an existing type Student <: Person - a Student is a Person that follows PA lessons can be single or multiple

Single inheritance In single inheritance a class can extend only one class Very restrictive condition, it can t represent complex systems Single inheritance doesn t help expressivity

Single inheritance implementation When class Student inherits from class Person the memory layout of Person is stacked over Student From Student we can access Person using a pointer to the super class (super)

Single inheritance implementation When class Student inherits from class Person the memory layout of Person is stacked over Student From Student we can access Person using a pointer to the super class (super) Student <: Person Person Student

Single inheritance (Example) class Person { string Name; int SayHello() { print Hello my name is.name ; } } class Sudent : Person { string Class; int SayHello() { super.sayhello(); print I am a.class. student ; } }

Multiple inheritance In multiple inheritance a class can extend as many classes as it likes Very expressive, it can represent complex systems Multiple inheritance has both conceptual and implementation issues

Multiple inheritance implementation When class JetCar inherits from class Car and class Jet the memory layout of the JetCar is stacked with the ones of Jet and Car From JetCar we can access Car, or Jet, using a pointer to the super desired class

Multiple inheritance implementation When class JetCar inherits from class Car and class Jet the memory layout of the JetCar is stacked with the ones of Jet and Car From JetCar we can access Car, or Jet, using a pointer to the super desired class We must specify the particular super class if both super classes have a method with the same name JetCar <: Jet, JetCar <: Car Jet Car JetCar

Multiple inheritance (Example 1) class Jet { void Fly() { /*...*/ } } class Car { void Drive() { /*..*/ } } class JetCar: Jet, Car { void FlyAndDrive() { Fly(); Drive(); } }

Multiple inheritance (Example 2) class Jet { void Stop() { /*...*/ } } class Car { void Stop() { /*..*/ } } class JetCar: Jet, Car { void Stop() { Jet::Stop(); Car::Stop(); } }

Multiple inheritance (diamond problem) Suppose: JetCar <: Jet and JetCar <: Car and Jet <: Mobile and Car <: Mobile Mobile has a method called MoveTo(x,y,z) Jet Mobile JetCar Car

Multiple inheritance (diamond problem) We can have two problems:

Multiple inheritance (diamond problem) We can have two problems: Memory layout is bigger (2 copies of Mobile) JetCar <: Jet, JetCar <: Car Jet <: Mobile, Car <: Mobile Mobile(Jet) Jet Mobile(Car) Car JetCar

Multiple inheritance (diamond problem) We can have two problems: Memory layout is bigger (2 copies of Mobile) If we have two copies of Mobile, when in JetCar we write Mobile::MoveTo(x,y,z) what happens? JetCar <: Jet, JetCar <: Car Jet <: Mobile, Car <: Mobile Mobile(Jet) Jet Mobile(Car) Car JetCar

Multiple inheritance solution to the diamond problem

Multiple inheritance solution to the diamond problem C++ by default follows each inheritance path separately, so a JetCar object would actually contain two separate Mobile objects. But if the inheritance from Mobile is virtual C++ takes care to have only one copy of Mobile Eiffel handles this situation by a select and rename directives: Jet can have part of the interface of Mobile and Car another part, or we can rename the method MoveTo(x,y,z) as MoveJetTo(x,y,z) in Jet and MoveCarTo(x,y,z) in Car Perl handles this by specifying the inheritance classes as an ordered list. If Jet is specified before Car we have only Mobile(Jet) in memory Python creates a classes tree that would be searched in left-first depth-first order and then removes all but the last occurrence of any repeated classes

C++ solution with virtual inheritance class Mobile {... }; class Jet: virtual public Mobile {... }; class Car: virtual public Mobile {... }; class JetCar: public Jet, public Car {... }; Mobile Jet Car JetCar

Mix-in inheritance In some languages (Java, C#), it is not possible to inherit from multiple classes Mix-in allows only to inherit from one class but to implement multiple interfaces

Mix-in inheritance In some languages (Java, C#), it is not possible to inherit from multiple classes Mix-in allows only to inherit from one class but to implement multiple interfaces No diamond problem! Partial code reuse and low expressivity (higher than single inheritance)

Mix-in inheritance problems No Implementation problems only expressivity problems

Mix-in inheritance problems No Implementation problems only expressivity problems Suppose that we want to program class JetCar (without Mobile) in a mix-in language We need to inherit from Jet and Car We decide to make Car an empty interface The implementation of the Car methods is in the JetCar class

Mix-in inheritance problems No Implementation problems only expressivity problems Suppose that we want to program class JetCar (without Mobile) in a mix-in language We need to inherit from Jet and Car We decide to make Car an empty interface The implementation of the Car methods is in the JetCar class If we create a new class BoatCar we need to implement all the Car methods again. The problem can be partially solved encapsulating all the methods in a Car Implementation class and calling them as external (ugly!!)

Mix-in inheritance: the story so far Mix-in is an old term used in various contexts

Mix-in inheritance: the story so far Mix-in is an old term used in various contexts In C++ mix-in represents the fact that one, or more, of the inherited classes is abstract: it s like an interface but some implemented methods are given Mix-in term was born with CLOS: in CLOS a mix-in is a piece of code that can be attached on-fly to an object Other systems using CLOS-style mix-in are: Flavours, Ruby, Perl 6, D, XOTcl, Python and ActionScript

Upcasting and downcasting Object systems have two useful ways to use inheritance

Upcasting and downcasting Object systems have two useful ways to use inheritance Upcast Downcast

Upcast Upcast provides the principal abstraction of inheritance: the inheriting class can be used in every occurrence of the inherited class Each Student can perform everything a Person can do. Student s := new Student(); Person p := s; It is not a real cast. The runtime simply use the Person part of the Student memory

Downcast Downcast permits to return from an upcast transformation When a Student exits from school became a simple Person but it can return to be a Student when it comes back to school Person p := new Student(); Student s := (Student)p; When a downcast is performed the subclass must be specified because a superclass can have a large number of subclasses As in upcast operation: this is not a real cast operation

Upcasting and downcasting internals When are (Up Down)cast operations checked?

Upcasting and downcasting internals When are (Up Down)cast operations checked? Upcast can be verified at compile time Downcast must be verified at run time

Late (By example) class Person { int SayHello() { print I am a Person ; } } class Student : Person { int SayHello() { print I am a Student ; } }... Person p := new Student(); p.sayhello(); //What does it print?

Late (By example) Which method is called?

Late (By example) Which method is called? It depends on the language: C++ calls Person::SayHello() Java (or C#) calls Student::SayHello()

Late The mechanism that associates the method Student::SayHello() to a Person object is called late

Late The mechanism that associates the method Student::SayHello() to a Person object is called late The main advantage of late is that we can create generic code that works on classes with a common ancestor A typical example is given by graphical interface classes that inherit from a Drawable class with a paint() Late introduces time overhead because it is checked at run time

Late The mechanism that associates the method Student::SayHello() to a Person object is called late The main advantage of late is that we can create generic code that works on classes with a common ancestor A typical example is given by graphical interface classes that inherit from a Drawable class with a paint() Late introduces time overhead because it is checked at run time In C++ we can use late only if we declare Person::SayHello() as a virtual method

Late implementation How can late identify the method to be invoked?

Late implementation How can late identify the method to be invoked? For each class using late we introduce a v-table To each virtual method is associated a slot in the v-table That pointer points to the body of the method to call Each instance of a class, in addition to class fields, has a pointer to the v-table: this costs space V-tables can be used to have information on the type of the object at run time: to have all the informations we need vtables also where they are not useful

Late example class Person { int SayHello() {... } } class Sudent : Person { int SayHello() {... } } Person p := new Person(); p.sayhello(); p VPointer Person VTable Person :: SayHello()

Late example class Person { int SayHello() {... } void TakeCar() {... } } class Sudent : Person { int SayHello() {... } } Student s := new Student(); s.takecar(); s VPointer Student VTable Person :: TakeCar()

Late example class Person { int SayHello() {... } void TakeCar() {... } } class Sudent : Person { int SayHello() {... } } Person p := new Student(); p.sayhello(); s VPointer Student VTable Student :: SayHello()

Abstract methods and classes Abstract methods are an high expressive system A method is abstract if it is declared in a class but the implementation is leaved to sub classes A class with one (or more) abstract method is called abstract class and cannot be instantiate. Only sub classes can be instantiated class Shape { abstract void Draw(); } class Square : Shape { void Draw() {... } }

permits to bind more than one object to a single name For instance: class A { int foo() {... } int foo(int i) {... } } The name foo identifies two different methods

internals

internals is a compiler trick! This process is called name mangling The compiler generates a different method name for each version of foo using the type in input (the output type must be the same!) For instance: foo() becomes foo v, and foo(int) becomes foo i When the method is invoked the compiler chooses the appropriate version of foo Sometime implicit conversions can lead to an ambiguity in the choice: a compiler error is raised

Operators overloading Operators overloading allows to give a different semantic to standard language operators as + and In some languages the overloading of the operators is performed in the same way of method overloading Conceptually the invocation of overloaded operators is rewritten as a method For instance we can create a Matrix class and define the sum operation on it (Example in C++): Matrix a,b,c;... c = a + b; // operator=(c, operator+(a, b))