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

Similar documents
CPS 506 Comparative Programming Languages. Programming Language

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

Concepts of Programming Languages

Lecture Notes on Programming Languages

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

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

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

Data Abstraction. Hwansoo Han

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

Subtyping (Dynamic Polymorphism)

Lecture 23: Object Lifetime and Garbage Collection

G Programming Languages - Fall 2012

Object-oriented Programming. Object-oriented Programming

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

CS558 Programming Languages Winter 2013 Lecture 8

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

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

Inheritance and object compatibility

Chapter 5. Names, Bindings, and Scopes

Chapter 9 :: Data Abstraction and Object Orientation

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

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

Object Oriented Paradigm

Names, Bindings, Scopes

Chapter 10 :: Data Abstraction and Object Orientation

Properties of an identifier (and the object it represents) may be set at

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

CSC 533: Organization of Programming Languages. Spring 2005

Java Object Oriented Design. CSC207 Fall 2014

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

What about Object-Oriented Languages?

10. Abstract Data Types

Lecture 15: Object-Oriented Programming

G Programming Languages - Fall 2012

CS 403/503 Exam 4 Spring 2015 Solution

Inheritance, Polymorphism and the Object Memory Model

C++ Important Questions with Answers

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

Programming Languages, Summary CSC419; Odelia Schwartz

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

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

1: Introduction to Object (1)

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Forth Meets Smalltalk. A Presentation to SVFIG October 23, 2010 by Douglas B. Hoffman

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

Lecturer: William W.Y. Hsu. Programming Languages

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

Object Oriented Paradigm Languages

Object-Oriented Concepts and Design Principles

CSE 307: Principles of Programming Languages

Implementing Subprograms

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 2: Review of Object Orientation

CS-202 Introduction to Object Oriented Programming

CMSC 132: Object-Oriented Programming II

Lecture 16: Object Programming Languages

Chapter 5 Names, Binding, Type Checking and Scopes

Thanks! Review. Course Goals. General Themes in this Course. There are many programming languages. Teaching Assistants. John Mitchell.

Object-Oriented Programming

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

Compaq Interview Questions And Answers

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

Abstract Data Types and Encapsulation Concepts

Object Oriented Programming. Java-Lecture 11 Polymorphism

Concepts of Programming Languages

CS-XXX: Graduate Programming Languages. Lecture 22 Class-Based Object-Oriented Programming. Dan Grossman 2012

The object-oriented approach goes a step further by providing tools for the programmer to represent elements in the problem space.

Programming Languages 2nd edition Tucker and Noonan"

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

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

Imperative Programming

Chapter 6 Introduction to Defining Classes

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

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

Chapter 9. Subprograms

The Essence of Object Oriented Programming with Java and UML. Chapter 2. The Essence of Objects. What Is an Object-Oriented System?

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Advanced Database Applications. Object Oriented Database Management Chapter 13 10/29/2016. Object DBMSs

Object Oriented Issues in VDM++

What is Inheritance?

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

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

StackVsHeap SPL/2010 SPL/20

Inheritance and Substitution (Budd chapter 8, 10)

Strict Inheritance. Object-Oriented Programming Spring 2008

Object-Oriented Design

How Developers Use the Dynamic Features of Programming Languages: The Case of Smalltalk

Inheritance and Polymorphism

Chapter 11. Abstract Data Types and Encapsulation Concepts

Overview. Elements of Programming Languages. Objects. Self-Reference

Chapter 9 Subprograms

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

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

C++ Inheritance and Encapsulation

Some instance messages and methods

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

OBJECT ORIENTED PROGRAMMING USING C++

Programming, numerics and optimization

CSE 303: Concepts and Tools for Software Development

Chapter 1: Object-Oriented Programming Using C++

Data Structures (list, dictionary, tuples, sets, strings)

Transcription:

Lecture 13: Object orientation Object oriented programming Introduction, types of OO languages Key concepts: Encapsulation, Inheritance, Dynamic binding & polymorphism Other design issues Smalltalk OO support in C++ and Java Object oriented programming builds on the basic idea that the data objects is the central concept, not the (sub)programs Key concepts Encapsulation (cf ADT) Inheritance Dynamic binding => inclusion polymorphism Paradigm Evolution 1 Procedural - 1950s-1970s (procedural abstraction) 2 Data-Oriented - early 1980s (data abstraction) 3 OOP - late 1980s (inheritance and dynamic binding) 1 2 Object oriented programming Introduction Computations are done by sending and responding to messages The messages corresponds to subprogram calls in imperative languages and function application in functional languages (Abstract) data types are called classes, their elements instances and subprograms methods Distinguish between class variables / instance variables, class methods / instance methods Languages can support OOP, or be truly object oriented Categories of languages that support OOP: 1 OOP support is added to an existing language C++ (also supports procedural and data-oriented programming) Ada 95 (also supports procedural and data-oriented programming) CLOS (also supports functional programming) Scheme (also supports functional programming) 3 4 Introduction OO and ADT:s 2 Support OOP, but have the same appearance and use the basic structure of earlier imperative languages Eiffel (not based directly on any previous language) Java (based on C++) C# 3 Pure OOP languages Smalltalk (old pioneer, first pure OO lang) Ruby (new) Object orientation may be seen as an attempt to generalize the ADT concept and avoid its problems The open-closed-paradox : we want modules to be open in order to be able to add components, but closed for the protection of the components Information hiding prevents the re-use of common parts in related ADT:s No support for creating families of ADT:s which are variants of each other If ADT A is extended to ADT B it may be ineffective not being able to use implementation details in A 5 6 1

OO and ADT:s - inheritance Object orientation tries to avoid the ADT-problems by using the concept inheritance Subclasses inherits variables and methods from superclasses A subclass may get access to otherwise hidden implemention details Advantages Modification and efficient creation of variants is easy Programs will get a tree- or graph-like class structure BUT: The advantages of ADT:s are diminished Inheritance (continued) Inheritance can be complicated by access controls to encapsulated entities A class can hide entities from its subclasses A class can hide entities from its clients A class can also hide entities for its clients while allowing its subclasses to see them Besides inheriting methods as is, a class can modify an inherited method The new one overrides the inherited one The method in the parent is overriden 7 8 Dynamic Binding Dynamic binding gives polymorphism A polymorphic variable can be defined that is able to reference (or point to) objects of a class and objects of any of its descendants When a class hierarchy includes classes that override methods and such methods are called through a polymorphic variable, the binding to the correct method will be dynamic Allows software systems to be more easily extended during both development and maintenance Method calls are dynamically bound to the correct method definition in the current subclass It gives us (a kind of) polymorphism class square (subclass) class shape (superclass) class circle (subclass) var x: shape; // x has type shape x = new shape(); xdraw(); // a standard shape is drawn x = new square(); xdraw(); // a square is drawn x = new circle(); xdraw(); // a circle is drawn 9 10 Design issues for OOPL:s The Exclusivity of Objects The exclusivity of objects Dynamic and static binding Type checking and polymorphism Are subclasses subtypes? Single and multiple inheritance Allocation and deallocation of objects Nested classes Everything is an object Advantage - elegance and purity Disadvantage - slow operations on simple objects Add objects to a complete typing system Advantage - fast operations on simple objects Disadvantage - results in a confusing type system (two kinds of entities) Include an imperative-style typing system for primitives but make everything else objects Advantage - fast operations on simple objects and a relatively small typing system Disadvantage - still some confusion because of the two type systems 11 12 2

Dynamic and Static Binding Should all binding of messages to methods be dynamic? If none are, you lose the advantages of dynamic binding If all are, it is inefficient Allow the user to specify? Implementation of dynamic binding For every object a table is created ("class instance record") which contains all local variables plus a reference to the virtual method table of the class class instance record VMT Instance variable 1 Instance variable m virtual method table method 1 method n Only one copy is needed The format is static for each class 13 14 Type Checking and Polymorphism Are Subclasses Subtypes? Polymorphism may require dynamic type checking of parameters and the return value Dynamic type checking is costly and delays error detection If overriding methods are restricted to having the same parameter types and return type, the checking can be static Does an is-a relationship hold between a parent class object and an object of the subclass? If a derived class is-a parent class, then objects of the derived class must behave the same as the parent class object A derived class is a subtype if it has an is-a relationship with its parent class Subclass can only add variables and methods and override inherited methods in compatible ways If so, it is always possible to substitute a subclass variable for a superclass variable 15 16 Two perspectives on inheritance Single vs multiple inheritance Module perspective Reuseability If B are like A with some extensions we can define B as a subclass of A Solves the open-closed paradox Handles equalities / individual differences between related objects in superclasses / subclasses Extension! Type perspective Decentralization; each variant of an operation is specified in the same module as the (sub)type Dynamic type binding Specialization! 17 Single inheritance: Every class has at most one superclass ( the super-/subclass relation can be shown as a tree) Multiple inheritance: A class may have several superclasses ( the super-/subclass relation creates an acyclic graph) Problems with multiple inheritance: Name collisions (what if several x s are inherited?) diamond inheritance doubles variables and methods A B C B' complicated relations between classes low readability potential inefficiency 18 3

Allocation and deallocation of Objects From where are objects allocated? If they are all heap-dynamic (explicitly created), references can be uniform thru a pointer or reference variable Simplifies assignment - dereferencing can be implicit If objects are stack dynamic, there is a problem with regard to subtypes Is deallocation explicit or implicit? Nested Classes If a new class is needed by only one class, there is no reason to define so it can be seen by other classes Can the new class be nested inside the class that uses it? In some cases, the new class is nested inside a subprogram rather than directly in another class Other issues: Which facilities of the nesting class should be visible to the nested class and vice versa? 19 20 Smalltalk A little smalltalk A pure OO-language (the first) Everything is objects A fullblown programming environment - the first complete GUI system Completely dynamic type binding All subclasses are subtypes (nothing can be hidden) No multiple inheritance All computation is through objects sending messages to objects Different syntax fac faculty,recursive self > 0 iftrue: [^ self * (self - 1) fac] iffalse: [^ 1] fac result i result i 1 [i < self] whiletrue: [i i + 1 result result * i ] ^result faculty, while-loop fac result faculty, for-loop result 1 2 to: self do: [:i result result * i] ^result 21 22 Smalltalk (continued) Evaluation of Smalltalk The syntax of the language is simple and regular Good example of power provided by a small language Slow compared with conventional compiled imperative languages Dynamic binding allows type errors to go undetected until run time Introduced the graphical user interface Greatest impact: advancement of OOP Support for OOP in C++ General Characteristics: Evolved from C and SIMULA 67 Among the most widely used OOP languages Mixed typing system Constructors and destructors Elaborate access controls to class entities 23 24 4

Support for OOP in C++ (continued) Evaluation A large and complex language! C++ provides extensive access controls (unlike Smalltalk) C++ provides multiple inheritance In C++, the programmer must decide at design time which methods will be statically bound and which must be dynamically bound Static binding is faster! Smalltalk type checking is dynamic (flexible, but somewhat unsafe) Because of interpretation and dynamic binding, Smalltalk is ~10 times slower than C++ Support for OOP in Java General Characteristics All data are objects except the primitive types All primitive types have wrapper classes that store one data value All objects are heap-dynamic, are referenced through reference variables, and most are allocated with new Inheritance Single inheritance supported only, but there is an abstract class category that provides some of the benefits of multiple inheritance (interface) Methods can be final (cannot be overriden) 25 26 Support for OOP in Java (continued) Summary Dynamic Binding In Java, all messages are dynamically bound to methods, unless the method is final (ie, it cannot be overriden, therefore dynamic binding serves no purpose) Static binding is also used if the methods is static or private both of which disallow overriding OO programming involves three fundamental concepts: ADTs, inheritance, dynamic binding Major design issues: exclusivity of objects, subclasses and subtypes, type checking and polymorphism, single and multiple inheritance, dynamic binding, explicit and implicit de-allocation of objects, and nested classes Smalltalk is a pure OOL C++ has two distinct type system (hybrid) Java is not a hybrid language like C++; it supports only OO programming C# is based on C++ and Java Ruby is a new pure OOP language; provides some new ideas in support for OOP Implementing OOP involves some new data structures 27 28 5