Lecture 15: Object-Oriented Programming

Similar documents
Lecturer: William W.Y. Hsu. Programming Languages

Data Abstraction. Hwansoo Han

Lecture 13: Complex Types and Garbage Collection

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

Lecture Notes on Programming Languages

CPS 506 Comparative Programming Languages. Programming Language

Subtyping (Dynamic Polymorphism)

Lecture 23: Object Lifetime and Garbage Collection

Object Oriented Paradigm

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

10. Object-oriented Programming. 7. Juli 2011

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

Chapter 10 :: Data Abstraction and Object Orientation

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

Lecture 7: Binding Time and Storage

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

CMSC 132: Object-Oriented Programming II

Chapter 9 :: Data Abstraction and Object Orientation

Abstract data types &

Abstract Data Types & Object-Oriented Programming

Lecture 12: Data Types (and Some Leftover ML)

C++ Important Questions with Answers

Lecture 5: Inheritance

CMSC 132: Object-Oriented Programming II

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

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

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

G Programming Languages - Fall 2012

Concepts of Programming Languages

Java Object Oriented Design. CSC207 Fall 2014

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

COMP 2355 Introduction to Systems Programming

CLASSES AND OBJECTS IN JAVA

Inheritance and Interfaces

CS558 Programming Languages

Objects and Classes. Basic OO Principles. Classes in Java. Mark Allen Weiss Copyright 2000

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

8. Object-oriented Programming. June 15, 2010

Object-oriented Programming. Object-oriented Programming

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

10. Abstract Data Types

Classes, Objects, and OOP in Java. June 16, 2017

Instantiation of Template class

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

Chapter 5. Names, Bindings, and Scopes

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

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

CS558 Programming Languages Winter 2013 Lecture 8

Object Oriented Programming. Java-Lecture 11 Polymorphism

CS558 Programming Languages

Inheritance, and Polymorphism.

CS 251 Intermediate Programming Inheritance

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

Assumptions. History

CS153: Compilers Lecture 11: Compiling Objects

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

Names, Scopes, and Bindings II. Hwansoo Han

Abstract Data Types and Encapsulation Concepts

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

Chapter 5 Names, Binding, Type Checking and Scopes

Critique this Code. Hint: It will crash badly! (Next slide please ) 2011 Fawzi Emad, Computer Science Department, UMCP

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

JAVA: A Primer. By: Amrita Rajagopal

Kickstart Intro to Java Part I

Messages, Instances and Initialization

1. An object of a class can be treated as an object of its corresponding class.

Lecture 10: Expression Evaluation

Runtime Support for OOLs Object Records, Code Vectors, Inheritance Comp 412

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

Short Notes of CS201

What about Object-Oriented Languages?

CSE 341, Autumn 2015, Ruby Introduction Summary

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

Java: introduction to object-oriented features

CS201 - Introduction to Programming Glossary By

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

CS558 Programming Languages

CS Programming I: Inheritance

Inheritance. Transitivity

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009

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

Iterator: A way to sequentially access ( traverse ) each object in a container or collection. - Access is done as needed, not necessarily at once.

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

TYPES, VALUES AND DECLARATIONS

Functional Programming

Programming 2. Object Oriented Programming. Daniel POP

CSE 307: Principles of Programming Languages

Written by John Bell for CS 342, Spring 2018

Evolution of Programming Languages

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Objects, Encapsulation, Inheritance (2)

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

Inheritance, Polymorphism and the Object Memory Model

Class 22: Inheritance

VIRTUAL FUNCTIONS Chapter 10

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

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Building custom components IAT351

Transcription:

Lecture 15: Object-Oriented Programming COMP 524 Programming Language Concepts Stephen Olivier March 23, 2009 Based on slides by A. Block, notes by N. Fisher, F. Hernandez-Campos, and D. Stotts

Object Oriented Programming Benefits Reduces conceptual load Fault containment Independence between components Makes interface design very important 2

Brief History Simula (60 s) : Objects, classes, etc. Smalltalk (70 s) : OO as we know it C++ (80 s) : OO meets C, first widespread OO language Java (90 s) : C/C++ syntax but OO-centric design and more... 3

Class-as-type A class can be viewed as a class-as-type, when it contains multiple variables and/or functions to manipulate these variables. 4

Public Variables Generally, people consider it bad form to put variables in the public space. Why? 5

Class-as-manager A class can be viewed as a class-as-manager when it is not directly used to contain variables but rather to manage other objects. 6

Public, Private, and Protected (in C++) Public fields are available to everyone Private fields are available only to the class Protected are available only to descendants. 7

Deriving Classes Classes can be derived from other class, and inherit their functions. This is a KEY component of object-oriented programming. Also opens possibility to have subtype polymorphism 8

class foo{ public int x; Inheritance private int y; protected int z; } class bar : foo { public int xy() {return x+y;} //invalid public int xz() {return x+z;} //valid }... bar bbb; bbb.x = 1; //valid bbb.y = 1; //invalid bbb.z = 1; //invalid 9

Scoping In order to refer to a function for a particular class, use the scope resolution operator :: class foo{ public int bar(); } int foo::bar(){ return 0;} 10

General Purpose Base Class Its possible to define a general purpose base class as the highest level of abstraction for example Object in Java 11

Modifying Base Class Methods Its possible to redefine base methods in derived classes class foo{ public int bar(); }... class qud : foo{ public int bar(){ return 1;}; } 12

Modifying Base Class Methods Sometimes better just to use base if possible and try to return errors class foo{ public int bar(); }... class qud : foo{ public int bar(){ try {return foo::bar();}...}; } 13

Protection Rules (in C++) Any class can limit visibility of its member A derived class can restrict the visibility of a base class, but never increase it A derived class that limits visibility of members of a base class as protected or private can restore the visibility of individual members by inserting a using declaration in protected or public portion of the derived class. 14

Java vs C++ Under Java derived classes cannot change the visibility from the base class But members can be redefined. Protected has a slightly different meaning. Under Java, protected means that it is visible by everything in the package and derived classes Without protected, it is visible by everything in the package, but not derived classes in other packages 15

Three Issues Regarding Constructers Choosing a constructor References and Values Execution Order 16

Choosing Two common ways: 1. Named constructors (Smalltalk, Eiffel) 2. Overloading (C++, Java, C#) Some languages (Ada95, Modula-3) don t initialize objects at elaboration time 17

Value and References Variables can refer to objects (Smalltalk, Java, Python, Ruby) or have values which are objects (C++) 18

References More elegant, but can cause additional overhead. Space required on heap Indirection to access heap Garbage collection 19

Objects as Values (C++) When variables are used as values in C++, a constructor can be implicitly called. foo b; //Calls foo::foo() foo b(10, x ); //calls foo:foo(int, char) 20

Costs of the Value Model foo a = b+c; foo t; t = b.operator+(c); foo a = t; foo a = b; a += c; // avoids need for t 21

Execution order When an object employs inheritance it constructs the base classes first foo::foo( foo_params ) : bar (bar_params) {...} foo ( foo_params ){ super (bar_parms)... } 22

Destructors Needed in OO languages with explicit heap management, e.g. C++ class foo { char * list; foo() { list = new char[100];} ~foo() { delete list }; } 23