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

Similar documents
Programming Languages 2nd edition Tucker and Noonan"

Concepts of Programming Languages

Data Abstraction. Hwansoo Han

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

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

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

CPS 506 Comparative Programming Languages. Programming Language

Java Object Oriented Design. CSC207 Fall 2014

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

Classes, subclasses, subtyping

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

Object Oriented Paradigm

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

Implementing Subprograms

Abstract data types &

Abstract Data Types & Object-Oriented Programming

Chapter 11. Abstract Data Types and Encapsulation Concepts

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

CLASSES AND OBJECTS IN JAVA

CMSC 132: Object-Oriented Programming II

Chapter 1: Object-Oriented Programming Using C++

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

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

10. Abstract Data Types

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

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

CSE 307: Principles of Programming Languages

Chapter 11. Abstract Data Types and Encapsulation Concepts

OBJECT ORIENTED PROGRAMMING USING C++

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

Chapter 11. Abstract Data Types and Encapsulation Concepts

Abstract Data Types and Encapsulation Concepts

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

Object-Oriented Programming Concepts

Inheritance, and Polymorphism.

CMSC 132: Object-Oriented Programming II

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

G Programming Languages - Fall 2012

JAVA MOCK TEST JAVA MOCK TEST II

Inheritance and Polymorphism

Day 6. COMP1006/1406 Summer M. Jason Hinek Carleton University

Inheritance. Transitivity

Short Notes of CS201

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

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

Software Architecture (Lesson 2) Object-Oriented Paradigm (1)

2. The object-oriented paradigm

Inheritance and Substitution (Budd chapter 8, 10)

CS201 - Introduction to Programming Glossary By

What are the characteristics of Object Oriented programming language?

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

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

Chapter 6 Introduction to Defining Classes

Lecture 23: Object Lifetime and Garbage Collection

Object Oriented Paradigm Languages

Object-oriented Programming. Object-oriented Programming

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Compiler construction 2009

Programming II (CS300)

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Chapter 11. Abstract Data Types and Encapsulation Concepts 抽象数据类型 与封装结构. 孟小亮 Xiaoliang MENG, 答疑 ISBN

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

Compaq Interview Questions And Answers

Programming overview

CMSC 4023 Chapter 11

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

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

Object Oriented Programming. Solved MCQs - Part 2

"Object Oriented" Programming using ML

Lecture Notes on Programming Languages

Object-Oriented Programming

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

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

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

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

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

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

G Programming Languages - Fall 2012


Java. Representing Data. Representing data. Primitive data types

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

CS304 Object Oriented Programming Final Term

CMSC330. Objects, Functional Programming, and lambda calculus

A declaration may appear wherever a statement or expression is allowed. Limited scopes enhance readability.

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

CSE 12 Week Eight, Lecture One

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

Fundamentals of Programming Languages

COURSE 2 DESIGN PATTERNS

Get Unique study materials from

Inheritance, Polymorphism and the Object Memory Model

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

Review sheet for Final Exam (List of objectives for this course)

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

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

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

Introduction to OOP. Procedural Programming sequence of statements to solve a problem.

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

Transcription:

Chapter 13 Object Oriented Programming

Contents 13.1 Prelude: Abstract Data Types 13.2 The Object Model 13.4 Java

13.1 Prelude: Abstract Data Types Imperative programming paradigm Algorithms + Data Structures = Programs Produce a program by functional decomposition Start with function to be computed Systematically decompose function into more primitive functions Stop when all functions map to program statements

Procedural Abstraction Concerned mainly with interface Function What it computes Ignore details of how Example: sort(list, length);

Data Abstraction Or: abstract data types Extend procedural abstraction to include data Example: type float Extend imperative notion of type by: Providing encapsulation of data/functions Example: stack of int's Separation of interface from implementation

Defn: Encapsulation is a mechanism which allows logically related constants, types, variables, methods, and so on, to be grouped into a new entity. Examples: Procedures Packages Classes

A Simple Stack in C Figure 13.1

A Simple Stack in C #include <stdio.h> struct Node { int val; struct Node* next; }; typdedef struct Node* STACK; STACK thestack = NULL;

int empty( ) { return thestack == NULL; } int pop ( ) { STACK temp = thestack; int result = thestack->val; thestack = thestack->next; free(temp); return result; }

int top( ) { return thestack ->val; } void push (int newval) { STACK temp = (STACK)malloc(sizeof( struct Node)); temp->val = newval; temp->next = thestack; thestack = temp; }

Problems Users cannot declare a variable of type stack. The code defines only a single instance of a stack. Restricted to a stack of integers.

A Stack Type in C with Better Encapsulation Figure 13.2

Implementation of Stack Type in C Figure 13.3

Now, users can declare a variable of type stack. just need to include the header file. Problems Permit accesses as a linked list rather than a real stack. Restricted to a stack of integers. Not easy to extend the type.

Goal of Data Abstraction Package Data type Functions Into a module so that functions provide: public interface defines type Ada introduces packages.

generic type element is private; package stack_pck is type stack is private; procedure push (in out s : stack; i : element); procedure pop (in out s : stack) return element; procedure isempty(in s : stack) return boolean; procedure top(in s : stack) return element;

private type node; type stack is access node; type node is record val : element; next : stack; end record; end stack_pck;

package body stack_pck is procedure push (in out s : stack; i : element) is temp : stack; begin temp := new node; temp.all := (val => i, next => s); s := temp; end push;

procedure pop (in out s : stack) return element is temp : stack; elem : element; begin elem := s.all.val; temp := s; s := temp.all.next; dispose(temp); return elem; end pop;

procedure isempty(in s : stack) return boolean is begin return s = null; end isempty; procedure top(in s : stack) return element is begin return s.all.val; end top; end stack_pck;

13.2 The Object Model Problems remained: Automatic initialization and finalization No simple way to extend a data abstraction Concept of a class Object decomposition, rather than function decomposition

Defn: A class is a type declaration which encapsulates constants, variables, and functions for manipulating these variables. A class is a mechanism for defining an abstract data type (ADT).

class MyStack { class Node { Object val; Node next; Node(Object v, Node n) { val = v; next = n; } } Node thestack; MyStack( ) { thestack = null; } boolean empty( ) { return thestack == null; }

Object pop( ) { Object result = thestack.val; thestack = thestack.next; return result; } Object top( ) { return thestack.val; } void push(object v) { thestack = new Node(v, thestack); } }

OO program: collection of objects which communicate by sending messages Defn: A language is object-oriented if it supports an encapsulation mechanism with information hiding for defining abstract data types, virtual methods, and inheritance

Some Terms Classes Determine type of an object Permit full type checking Method: function in OO. Class constructor: allocate heap space and initialize the object. Class destructor: final operations before destroying the object.

Some Terms (cont.) Client of a class C Any other class or method that declares or uses an object of class C Class methods (Java static methods) invoked through the class name exception: constructor is invoked through the new operation. Instance methods invoked through an object Inner class A class defined inside another class

class MyStack { class Node { Object val; Node next; Node(Object v, Node n) { val = v; next = n; } } Node thestack; MyStack( ) { thestack = null; } boolean empty( ) { return thestack == null; }

Object pop( ) { Object result = thestack.val; thestack = thestack.next; return result; } Object top( ) { return thestack.val; } void push(object v) { thestack = new Node(v, thestack); } }

Visibility of a variable or method public visible to any client and subclass of the class protected visible only to a subclass of the class (Java has a special meaning of protected classes: private visible to every class in the same package) visible only to the current class

Inheritance Class hierarchy Subclass, parent or super class is-a relationship A stack is-a kind of a list So are: queue, deque, priority queue has-a relationship Identifies a class as a client of another class Aggregation A class is an aggregation if it contains other class objects

Single inheritance A subclass can have only one parent class The class hierarchy hence forms a tree. Rooted in a most general class: Object Inheritance supports code reuse Single inheritance languages: Smalltalk, Java

Inheritance of Stack in Java: Good or bad design? Why? Alternative design: Stack has a private vector object. Class Stack { private Vector stack; public push(...){...} public pop(...) {...} }

A common practice Make instance variables private Allow accesses only through the use of public or protected methods Good or bad? Why? Minimize needed changes if the class is modified.

Multiple Inheritance Allows a class to be a subclass of zero, one, or more classes. Class hierarchy is a directed graph Examples: C++, Python

Example ClosedFigure Circle Drawable Adv: facilitates code reuse Disadv: more complicated semantics A method is defined in both parent classes, which one is inherited by the child? Python uses left-to-right, depth-first search search child, then parent1 and its super classes, then parent2 and its super classes, and so on.

Polymorphism Polymorphic - having many forms Defn: In OO languages, it s late binding of a call to one of several different implementations of a method in an inheritance hierarchy. Virtual Method: A method whose behavior can be overridden within an inheriting class by a function with the same signature. In Java, by default, all methods are virtual.

Consider the call: obj.m( ); obj of type T All subtypes must implement method m( ) In a statically typed language, verified at compile time Actual method called can vary at run time depending on actual type of obj

for (Drawable obj : mylist)! obj.paint( ); // paint method invoked varies // each graphical object paints itself // essence of OOP

Defn: A subclass method is substitutable for a parent class method if the subclass s method performs the same general function. Thus, the paint method of each graphical object must be transparent to the caller. The code to paint each graphical object depends on the principle of substitutability.

Templates or Generics A kind of class generator Can restrict a Collections class to holding a particular kind of object Defn: A template defines a family of classes parameterized by one or more types.

ArrayList<Drawable> list = new ArrayList<Drawable> ();... for (Drawable d : list)! d.paint(g);