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

Similar documents
Object-oriented Programming. Object-oriented Programming

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

Data Abstraction. Hwansoo Han

CPS 506 Comparative Programming Languages. Programming Language

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

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

Data types. Definitions Aggregate constructors User-defined type definitions Types and storage Types and subtypes Type systems and type checking

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

Chapter 1: Object-Oriented Programming Using C++

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

Inheritance and Substitution (Budd chapter 8, 10)

Subtyping (Dynamic Polymorphism)

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

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

Inheritance. OOP: Inheritance 1

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Inheritance, Polymorphism and the Object Memory Model

Concepts of Programming Languages

C++ Important Questions with Answers

COSC252: Programming Languages: Abstraction and OOP. Jeremy Bolton, PhD Asst Teaching Professor. Copyright 2015 Pearson. All rights reserved.

Object-Oriented Paradigm

Object Oriented Paradigm Languages

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

Java Object Oriented Design. CSC207 Fall 2014

G Programming Languages - Fall 2012

Object-Oriented Concepts and Design Principles

C++ Inheritance and Encapsulation

Inheritance and object compatibility

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

Lecture Notes on Programming Languages

Fundamentals of Programming Languages

Inheritance and Interfaces

Inheritance. Finally, the final keyword. A widely example using inheritance. OOP: Inheritance 1

C++ Programming: Polymorphism

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

Chapter 11. Abstract Data Types and Encapsulation Concepts

Homework 6. Reading. Problems. Handout 7 CS242: Autumn November

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

OBJECT ORIENTED PROGRAMMING USING C++

What are the characteristics of Object Oriented programming language?

What is Inheritance?

Inheritance, and Polymorphism.

Object Oriented Programming

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Programming Languages 2nd edition Tucker and Noonan"

CS 520 Theory and Practice of Software Engineering Fall 2017

10. Abstract Data Types

CS 320 Introduction to Software Engineering Spring March 06, 2017

CS-202 Introduction to Object Oriented Programming

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

Topics. Modularity and Object-Oriented Programming. Dijkstra s Example (1969) Stepwise Refinement. Modular program development

CS558 Programming Languages Winter 2013 Lecture 8

Types. What is a type?

Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt

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

Procedure and Object- Oriented Abstraction

Today s lecture. CS 314 fall 01 C++ 1, page 1

CS 520 Theory and Practice of Software Engineering Fall 2018

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

Introduction to Object-Oriented Programming

Abstract Data Types and Encapsulation Concepts

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

Some instance messages and methods

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

Chapter 9 :: Data Abstraction and Object Orientation

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

CMSC 132: Object-Oriented Programming II

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

Object typing and subtypes

Lecture 5: Inheritance

Strict Inheritance. Object-Oriented Programming Spring 2008

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

CGS 2405 Advanced Programming with C++ Course Justification

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

ITI Introduction to Computing II

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Chapter 10 :: Data Abstraction and Object Orientation

Object Oriented Paradigm

CSE 307: Principles of Programming Languages

Inheritance. Transitivity

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

CS 251 Intermediate Programming Inheritance

C++ (classes) Hwansoo Han

Information System Design (IT60105)

C++ Yanyan SHEN. slide 1

Lecture 23: Object Lifetime and Garbage Collection

Lecturer: William W.Y. Hsu. Programming Languages

ITI Introduction to Computing II

- couldn t be instantiated dynamically! - no type or other method of organizing, despite similarity to

Abstract data types &

Abstract Data Types & Object-Oriented Programming

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

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

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Transcription:

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

What is an OO language? In Ada and Modula 2 one can define objects encapsulate a data structure and relevant operations in a construct data structure hidden to clients One can define ADTs Possible to define generic objects and ADTs Is this enough to call them object-oriented? Ghezzi&Jazayeri: OO Languages 2

but what is an OO language? OO languages must support: abstract data type definitions inheritance (inclusion) polymorphism dynamic binding of method invocations to bodies (also called dynamic dispatching) the method called on an object depends on the object s dynamic type, although its static type is used for type checking to ensure type safety Ghezzi&Jazayeri: OO Languages 3

Jargon Often procedures/functions encapsulated in an object are called methods, or member functions and procedure/function call sending a message To respond to a message sent to an object O, object O invokes an appropriate method Ghezzi&Jazayeri: OO Languages 4

class stack{ public: Inheritance (in C++) stack(); {top = 0;} //constructor void push(int) {s[top++] = i;}; int pop() {return s[--top];}; protected: int top; private: int s[100]; }; class counting_stack : public stack { public: int size(){return top;}; //# of elem. on the stack }; Ghezzi&Jazayeri: OO Languages 5

Polymorphism A stack reference can refer to a counting stack (but not vice-versa) stack* sp = new stack; counting_stack* csp = new counting_stack;... sp = csp; //ok csp = sp; //compiler error (strong typing: csp -> size???) WARNING: in C++ different behavior for automatic objects (unlike pure OO languages) stack s; counting_stack cs; s = cs; //ok but with COERCION!!! cs = s; // compiler error Ghezzi&Jazayeri: OO Languages 6

Dynamic binding stack* sp = new stack; counting_stack* csp = new counting_stack;... sp->push( ); //stack::push csp->push( ); //counting_stack::push sp = csp; sp->push( ); //which push???? The operation to be invoked should be the one defined by the object s dynamic type Ghezzi&Jazayeri: OO Languages 7

Inheritance and type system We wish a subclass to define a subtype NB: type, set (better: algebra), class should be the same thing but... The relation between a type and a subtype is substitutability If we wish to achieve strong typing and polymorphism, the subtype can add new operations redefine operations preserving contravariance of input (new domain is a superset of old one) and covariance of output (new range is a subset of old one) parameters in C++, Java, Object Pascal, and Modula3 they must be the same type; (NB: Java 1.5 admits covariance of output) Eiffel and Ada require covariance of both parameter and result Ghezzi&Jazayeri: OO Languages 8

Virtual functions and dynamic binding class student{ public:... virtual void print(){...}; }; student* s; college_student* cs;... s->print(); //calls student::print() s = cs; // okay s->print(); //calls college_student::print() class college_student: public student{ void print() {... // specific print for college_student } }; Ghezzi&Jazayeri: OO Languages 9

Inheritance Types organized in a hierarchy Subtype is-a parent type counting_stack is-a stack Subtypes can add features and redefine virtual member functions Multiple vs single inheritance Ghezzi&Jazayeri: OO Languages 10

Multiple inheritance root child1 child2 grandchild It can be necessary to redefine (name clashes) and undefine (to avoid duplication of inherited entities) C++ supports multiple inheritance Java does not (but can inherit from multiple interfaces Ghezzi&Jazayeri: OO Languages 11

Abstract classes class shape{ public: void draw() = 0; // these are pure virtual functions void move(...) = 0; void hide() = 0; point center; }; class rectangle: public shape{ private: float length, width; //specific data for rectangle public: void draw() {...}; //impl. for derived pure virtual function void move(...) {...}; void hide() {...}; }; Ghezzi&Jazayeri: OO Languages 12

Class structure class C { public: // accessible to the general public protected: // accessible to members and friends and // to members and friends of derived classes only // and (for Java) to classes of the same package private: // accessible to members and friends only }; Ghezzi&Jazayeri: OO Languages 13

Pure OO design Decomposition is based ONLY on classes A class is both a module and a type Classes may be generic Classes may be abstract Relationships among modules (classes) USES INHERITS Ghezzi&Jazayeri: OO Languages 14

Pure OO language Ada and Modula 2 are not object oriented they are sometimes called object based C++ is not purely object oriented: it supports also functions and functional decomposition Java is purely object oriented Ghezzi&Jazayeri: OO Languages 15

Smalltalk Fully dynamic: objects carry type at r.t. Syntax for method invocation suggestive of message sending mypoint xmove T lowerbound: 6 upperbound: 95 Class variables and methods Single inheritance Ghezzi&Jazayeri: OO Languages 16

Variables and dynamic typing Variables are uniformly refs to objects Refs are untyped: messages sent ok if object bound to ref can respond Dynamic polymorphism Ghezzi&Jazayeri: OO Languages 17

Pervasive role of objects 1 is an object a class is an object (of class class) that can respond to instantiation mypoint <- point new msg new sent to class point; assignment to mypoint control structures are objects programsviewedasdata Ghezzi&Jazayeri: OO Languages 18

Problem of size of private types If stack is an ADT which exports a type stack, e.g. In Ada and Modula, a module that exports a type stack In C++, it is the class itself How to handle a client declaration: s: stack; //how much memory to allocate? Ghezzi&Jazayeri: OO Languages 19

ADTs and modularity s: stack define stack and export ops Ghezzi&Jazayeri: OO Languages 20

Size of an ADT... Compiler has to know the size at client side even though it is not part of spec C++ and Ada require the representation of private type in the specification Java uses pointers to ADTs, pointer is of fixed size Modula s opaque export allows access through pointer without revealing representation Ghezzi&Jazayeri: OO Languages 21

Problem of assignment of private types Assignment and equality tests are fundamental operations of types. Does an arbitrary ADT support them? C++ provides default (memberwise) operations but user can redefine the operations Ada declares a type as private (supports assignment and equality) and limited private (does not) Ghezzi&Jazayeri: OO Languages 22

The case of Java All objects are implicitly accessible via pointers Copying an object copies the pointer, i.e., object is shared Ghezzi&Jazayeri: OO Languages 23