Object-oriented Programming. Object-oriented Programming

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

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

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

G Programming Languages - Fall 2012

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

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

Subtyping (Dynamic Polymorphism)

Java Object Oriented Design. CSC207 Fall 2014

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

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

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

Inheritance, Polymorphism and the Object Memory Model

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

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

More C++ : Vectors, Classes, Inheritance, Templates

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Some instance messages and methods

ECE 3574: Dynamic Polymorphism using Inheritance

CPS 506 Comparative Programming Languages. Programming Language

CS-202 Introduction to Object Oriented Programming

Data Abstraction. Hwansoo Han

Chapter 10 Object-Oriented Programming

CMSC 132: Object-Oriented Programming II

Conformance. Object-Oriented Programming Spring 2015

Inheritance and object compatibility

Object Oriented Paradigm

C++ (classes) Hwansoo Han

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

CSE 307: Principles of Programming Languages

Chapter 10 Classes Continued. Fundamentals of Java

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

The Java Programming Language

References: Chapters 10 and 11 Chapters 8, and 12( 2 and 3)

Concepts of Programming Languages

Lecture Notes on Programming Languages

Object Oriented Programming. Java-Lecture 11 Polymorphism

Object-Oriented Paradigm

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

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

VALLIAMMAI ENGINEERING COLLEGE

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

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

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

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

What about Object-Oriented Languages?

Types and Classes. I II From predefined (simple) and user-defined (composite) types via Abstract data types

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

Inheritance and Polymorphism

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

Chapter 5 Object-Oriented Programming

Advanced oo concepts Specialization of behaviour? Multiple inheritance - alternatives to. Inner classes Classes

Inheritance and Polymorphism

Types. What is a type?

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

CS 403/503 Exam 4 Spring 2015 Solution

Objects, Encapsulation, Inheritance (2)

Inheritance. Transitivity

CLASSES AND OBJECTS IN JAVA

Programming II (CS300)

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

Inheritance, and Polymorphism.

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

Practice for Chapter 11

What are the characteristics of Object Oriented programming language?

Programming II (CS300)

OBJECT ORİENTATİON ENCAPSULATİON

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

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

25. Generic Programming

Class, Variable, Constructor, Object, Method Questions

Fundamentals of Programming Languages

C++ Programming: Polymorphism

Object typing and subtypes

Object Oriented Programming. Solved MCQs - Part 2

Objects. Deian Stefan (Adopted from my & Edward Yang s CS242 slides)

Polymorphism. Zimmer CSCI 330

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

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

Instantiation of Template class

CH. 2 OBJECT-ORIENTED PROGRAMMING

Java: introduction to object-oriented features

Get Unique study materials from

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

Inheritance and Substitution (Budd chapter 8, 10)

Short Notes of CS201

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

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

Example: Count of Points

CS201 - Introduction to Programming Glossary By

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

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Programmiersprachen (Programming Languages)

Object Oriented Paradigm Languages

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

Chapter 11 Object and Object- Relational Databases

10. Object-oriented Programming. 7. Juli 2011

OBJECT ORIENTED PROGRAMMING USING C++

Transcription:

2014-06-13 Object-oriented Programming Object-oriented Programming

2014-06-13 Object-oriented Programming 1 Object-oriented Languages object-based: language that supports objects class-based: language that supports classes object-oriented: object-based language that additionally supports the definition of abstract data types, inheritance, inclusion polymorphism (subtyping), dynamic binding of routine invocations to routines terminology: objects are instances of classes, objects contain object/instance variables and support methods, messages are sent to objects

2014-06-13 Object-oriented Programming 2 Derived Class in C++ class stack { void push(int) { elements[top++] = i; } int pop() { return elements[--top]; } private: int elements[100]; int top = 0; }; class counting_stack : public stack { int size(); //return number of elements on the stack };

2014-06-13 Object-oriented Programming 3 Polymorphism and Dynamic Binding stack s; // not polymorph counting_stack cs; stack* sp = new stack(); //polymorph counting_stack* csp = new counting_stack();... sp = csp; // supported csp = sp; // not supported; statically unknown s = cs; // supported; converted to stack cs = s; // not supported; cs.size() undefined... sp->push(...); //stack::push csp->push(...); //counting_stack::push sp = csp; sp->push(...); //which push?

2014-06-13 Object-oriented Programming 4 Subclass Versus Subtype principle of substitutability: an instance of a subtype can be used where an instance of a supertype is expected subclass is a subtype if (simplified): object variables and methods are only added (not removed) and methods are redefined only in a compatible way: they have the same or a compatible signature (statically checkable, but not always sufficient) and methods behave equivalently (not statically checkable)

2014-06-13 Object-oriented Programming 5 Strong Types and Polymorphism class base {...}; class derived: public base {...};... base* b; derived *d; under which condition can an assignment b = d not lead to a type error? example: derived is subtype of base if derived can be used wherever base is expected such that no type error can arise are these conditions statically checkable?

2014-06-13 Object-oriented Programming 6 Method Overriding class polygon { polygon(...) {...} // constructor virtual float perimeter() {...}; //virtual... } class square: public polygon { square(...) {...} // constructor float perimeter() {...}; // virtual, overridden... }

2014-06-13 Object-oriented Programming 7 Contra-variant Parameter Types class base { virtual void fnc(s1 par) {...} }; class derived: public base { void fnc(s2 par) {...} };... base* b; derived* d; s1 v1; s2 v2; if(...) b = d; b->fnc(v1); // what if b is of type derived?

2014-06-13 Object-oriented Programming 8 Co-variant Result Types class base { virtual t1 fnc(...) {...} }; class derived: public base { t2 fnc(...) {...} };... base* b; derived* d; t0 v0; if(...) b = d; v0 = b->fnc(...); // what if b is of type derived?

2014-06-13 Object-oriented Programming 9 Co-variant Problem class point { float x, y; bool equal (point p) { return x == p.x && y == p.y; } }; class colorpoint : public point { int color; bool equal (colorpoint p) // error, co-variant { return x == p.x && y == p.y && color == p.color; } };

2014-06-13 Object-oriented Programming 10 Characteristics of C++ constructors initialize objects (base class first) destructors clean up (base class last) automatic object: automatically allocated and deallocated on stack objects on heap explicitly allocated and deallocated; corresponding functions new and delete are programmable static variables shared by all objects of class, static allocation assignment and equality operations programmable by overloading of = and ==

2014-06-13 Object-oriented Programming 11 Virtual Funtions in C++ class student { virtual void print() {...} }; class college_student: public student { void print() {...} };... student* s; college_student* cs;... s->print(); // calls student::print() s = sc; // okay s->print(); // calls college_student::print()

2014-06-13 Object-oriented Programming 12 Pure Virtual Funtions in C++ class shape { void draw() = 0; // pure virtual function void move(...) = 0; void hide() = 0; point center; }; class rectangle : public shape { private: float length, width; void draw() {... }; // impl. of derived function void move(...) {... }; void hide() {... }; };

2014-06-13 Object-oriented Programming 13 Inheritance and Visibility in C++ class stack { stack() { top = 0 }; // constructor void push(int i) { s[top++] = i; }; int pop() { return s[--top]; }; protected: int top; private: int s[100]; }; class counting_stack : public stack { int size() { return top; }; };

2014-06-13 Object-oriented Programming 14 Polymorphism in C++ genericity (= parametric polymorphism) by templates statically resolved (by heterogeneous translation) inclusion polymorphism supported by multiple inheritance statically and dynamically resolved overloading of functions and operators statically resolved explicit and implicit casts, run-time type information (rtti) semantic action at run-time

2014-06-13 Object-oriented Programming 15 Package and Tagged Record in Ada package Planan_Objects is type Planar_Object is tagged record X, Y: Float := 0.0; end record; function Distance (O: Planar_Object) return Float; procedure Move (O: in out Planar_Object; DX, DY: Float); procedure Draw (O: Planar_Object); end Planar_Objects;

2014-06-13 Object-oriented Programming 16 Extending Tagged Record in Ada with Planar_Objects; use Planar_Objects; package Special_Planar_Shapes is type Point in new Planar_Object with null record; procedure Draw (P: Point); type Circle is new Planar_Object with record Radius: Float; end record; procedure Draw (C: Circle); type Rectangle is new Planar_Object with record Length, Width: Float; end record; procedure Draw (T: Rectangle); end Special_Planar_Shapes;

2014-06-13 Object-oriented Programming 17 Use of Tagged Records in Ada O1: Planar_Object; O2: Planar_Object := (1.0, 1.0); C: Circle := (3.0, 4.5, 6.7);... O1 := Planar_Object(C); // type cast C := (O2 with 6.7) procedure Process_Shapes (O: Planar_Object Class) is... begin... Draw(O);... // dynamic binding end Process_Shapes; type Planar_Object_Ptr is access Planar_Object Class;

2014-06-13 Object-oriented Programming 18 Abstract Type in Ada package Planan_Objects is type Planar_Object is abstract tagged null record; function Distance (O: Planar_Object) return Float is abstract; procedure Move (O: in out Planar_Object; DX, DY: Float) is abstract; procedure Draw (O: Planar_Object) is abstract; end Planar_Objects;

2014-06-13 Object-oriented Programming 19 Polymorphism in Ada supports genericity including bounded genericity statically resolved inclusion polymorphism supported by single inheritance statically and dynamically resolved (by class-wide types) overloading of routines and operators statically resolved only explicit casts, type comparison at run-time semantic actions at run-time

2014-06-13 Object-oriented Programming 20 Classes and Inheritance in Eiffel class A feature fnc(t: TI): TO is... do... end -- fnc end -- class A class B inherit A redefine fnc feature fnc(s: SI): SO is... do... end -- fnc -- supports co-variant parameter types, not type-safe end -- class B... a: A; b: B;... create b; -- old syntax:!!b; a := b;... a.fnc(x)... -- dynamic binding

2014-06-13 Object-oriented Programming 21 Polymorphism in Eiffel supports genericity including bounded genericity statically resolved inclusion polymorphism supported by multiple inheritance dynamically resolved (expanded objects statically) co-variant parameter types can result in exceptions overloading of routines and operators statically resolved checked assignment attempt (x?= y) semantic action at run-time

2014-06-13 Object-oriented Programming 22 Characteristics of Smalltalk classes are objects that specify variables and methods of their instances variables are untyped references natural polymorphism single inheritance; methods corresponding to a messages are dynamically searched from bottom to top in class hierarchy syntax: object followed by message; examples: declaration: value call: setzero value (unary) declaration: > param call: x > y (binary) declaration: from: f to: t call: s from: x to: y (keyword) objects are created explicitly (mypoint <- point new), deallocation by garbage collector

2014-06-13 Object-oriented Programming 23 Examples in Smalltalk [x <- 0. y <- 0] value setzero <- [x <- 0. y <- 0]. setzero value x > y iftrue: [max <- x] iffalse: [max <- y] 10 timesrepeat: [x <- x + a] [x < b] whiletrue: [x <- x + a]

2014-06-13 Object-oriented Programming 24 Polymorphism in Smalltalk genericity not necessary because variables have no declared type dynamically resolved as side-effect of dynamic binding inclusion polymorphism supported independent from inheritance dynamically resolved by method search no overloading cast not necessary, type comparison at run-time semantic actions at run-time

2014-06-13 Object-oriented Programming 25 Characteristics of Java all objects allocated on heap, deallocated by garbage collector programs translated to portable JVM code (idea was to use Java as language in networks, but rarely used that way) single inheritance of classes and multiple inheritance of interfaces variables and methods belong to objects or classes differentiation between classes and packages support of final classes and methods predefined class Object contains equals, clone,...

2014-06-13 Object-oriented Programming 26 Clases and Inheritance in Java abstract class PlanarObject { protected float x, y; public float distance() {... } public void move(float x, y) {... } public abstract void draw(); } class Circle extends PlanarObject { private float radius; public void draw() {... } } class Rectangle extends PlanarObject { private float length, width; public void draw() {... } }

2014-06-13 Object-oriented Programming 27 Interfaces in Java interface Dictionary { void insert(string c; int i); int lookup(string c); } class ArrayDict implements Dictionary { private String[] names; private int[] values; private int size = 0; public void insert(string c; int i) {... } public int lookup(string c) {... } } class ListDict extends SomeList implements Dictionary {... }

2014-06-13 Object-oriented Programming 28 Polymorphism in Java genericity including bounded genericity since version 1.5 statically resolved inclusion polymorphism supported by single/multiple inheritance dynamically resolved (statically when private, final or static) overloading of routines, but no operators statically resolved checked casts, type comparison at run-time semantic actions at run-time