Rushikesh K Joshi. Department of Computer Science and Engineering Indian Institute of Technology Bombay

Similar documents
CS 617 Object Oriented Systems Lecture 5 Classes, Classless World:Prototypes, Instance Variables, Class Variables, This/Self 3:30-5:00 pm Thu, Jan 17

CS 617 Object Oriented Systems Lecture 3 Object Abstractions, Encapsulation, Abstract Data Types 3:30-5:00pm Thu, Jan 10

Rushikesh K Joshi. Department of Computer Science and Engineering Indian Institute of Technology Bombay

Chapter 5 Object-Oriented Programming

CS250 Final Review Questions

CPSC 427a: Object-Oriented Programming

C++ Modern and Lucid C++ for Professional Programmers

CPSC 427: Object-Oriented Programming

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

Object-oriented basics. Object Class vs object Inheritance Overloading Interface

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

Inheritance and Polymorphism

Inheritance and Polymorphism

C++ Inheritance and Encapsulation

Midterm Exam 5 April 20, 2015

An Introduction to Object Orientation

Administrivia. IBM Info Session Date: Wed,, Jan 13 Time: 5:30 7 pm Location: Wesbrook 100

G Programming Languages - Fall 2012

VIRTUAL FUNCTIONS Chapter 10

Computer Programming

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

Inheritance and Polymorphism

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Introduction to Object-Oriented Programming

Lecture 10: Interfaces

Type Hierarchy. Lecture 6: OOP, autumn 2003

Software Engineering Fall 2015 (CSC 4350/6350) TR. 5:30 pm 7:15 pm. Rao Casturi 11/03/2015

Strict Inheritance. Object-Oriented Programming Spring 2008

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

CS61B Lecture #7. Announcements:

Concepts of Programming Languages

2. The object-oriented paradigm

CS 61B Data Structures and Programming Methodology. July 2, 2008 David Sun

Object-Oriented Analysis, Design and Implementation. Case Study Part II

Assertions. Assertions - Example

Distributed Real-Time Control Systems. Lecture 14 Intro to C++ Part III

CS200: Advanced OO in Java

Inheritance, and Polymorphism.

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

What about Object-Oriented Languages?

CS-202 Introduction to Object Oriented Programming

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

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

C++ Important Questions with Answers

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

Java Object Oriented Design. CSC207 Fall 2014

CS250 Final Review Questions

COP 3530 Discussion Session #2 Ferhat Ay

Function Overloading and Overriding this keyword static members inline functions Passing arguments by values and reference

CSE 303: Concepts and Tools for Software Development

Advanced JML Erik Poll Radboud University Nijmegen

CSC9T4: Object Modelling, principles of OO design and implementation

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

Ramana Isukapalli W3101: Programming Languages C++

CLASSES AND OBJECT CHAPTER 04 CLASS XII

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

CS 338 The Enhanced Entity-Relationship (EER) Model

Inheritance. Like father, like son

CS61B Lecture #5: Arrays and Objects

Applications. April 12, Indian Institute of Space Science and Technology. MA122 - Computer Programming and. Applications.

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

More on Inheritance. Interfaces & Abstract Classes

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

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

CPS 506 Comparative Programming Languages. Programming Language

What are the characteristics of Object Oriented programming language?

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

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

UML Fundamental. OutLine. NetFusion Tech. Co., Ltd. Jack Lee. Use-case diagram Class diagram Sequence diagram

Object-Oriented Design

Programming in C++: Assignment Week 6

ECE 122. Engineering Problem Solving with Java

CS111: PROGRAMMING LANGUAGE II

CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism

University of Swaziland Department Of Computer Science Supplementary Examination JULY 2012

Strict Inheritance. Object-Oriented Programming Winter

Polymorphism. Arizona State University 1

Programming overview

Object Oriented Design

ECE 122. Engineering Problem Solving with Java

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Programming II (CS300)

Chapter 8 The Enhanced Entity- Relationship (EER) Model

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

Chapter 8: Class and Method Design

ITI Introduction to Computing II

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

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

Introduction to Unified Modelling Language (UML)

Object Oriented Programming. Java-Lecture 11 Polymorphism

Computer Science II CSci 1200 Lecture 24 C++ Inheritance and Polymorphism

Object-Oriented Design

Encapsulation. Mason Vail Boise State University Computer Science

Basic Object-Oriented Concepts. 5-Oct-17

Subtyping (Dynamic Polymorphism)

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Week 5-1: ADT Design

IS 0020 Program Design and Software Tools

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

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

Transcription:

CS 617 Object Oriented Systems Lecture 6 Classes Implementing Interfaces Open-Closed Principle Self References (This) 3:30-5:00 pm Mon, Jan 21 Rushikesh K Joshi Department of Computer Science and Engineering Indian Institute of Technology Bombay

Outline 1 Classes: Implementing Interfaces 2 3 4 5

Outline 1 Classes: Implementing Interfaces 2 3 4 5

Implementing Interfaces A class provides an implementation of an interface. An interface: Defines a set of messages through a set of abstract member functions (only the type signatures) A Class: Provides their implementations, i.e. the method bodies and it exports an interface. Distinction between Messages and Methods

Interfaces: Explicit Vs. Implicit I

Interfaces: Explicit Vs. Implicit II Class Account { public: int balance(): int withdraw (int amount); int deposit (int amount); } The interface is embedded in class description in the above example. Everything kept in public visibility contribute to the interface.

Interfaces: Explicit Vs. Implicit III What s the meaning of exporting a variable through the interface?

Class Implementing Multiple Interfaces I interface Send { public void send (int value); } interface Receive { public int receive (); } class UniChannel implements Send, Receive { int buffer; } public void send (int value) {buffer=value;} public void receive (int value) {return value;}

Class Implementing Multiple Interfaces II

Separating Interfaces from Classes Interfaces provide the protocols for interactions Classes act as implementors Application classes can be implemented purely in terms of interfaces Application classes can be fully unaware of implementation classes Implementations classes can vary without having to change the application classes that use implementation classes Implementation classes can also be changed without having to change the application classes that use them

Role Modeling with Multiple Interfaces Common implementation class The implementation class implements many interfaces Application classes get restricted contracts through a narrow window of the interface that they know

Outline 1 Classes: Implementing Interfaces 2 3 4 5

Partial Implementations No Implementation == Interface

as Interfaces I #include<iostream> using std::cout; class Channel { public: virtual void send(int data) = 0; virtual int receive () = 0; }; class BufferedChannel: public Channel { int buffer[1]; public: BufferedChannel () { }; virtual void send(int data);

as Interfaces II virtual int receive (); }; void BufferedChannel::send (int data) {buffer[0]=data;} int BufferedChannel::receive () {return buffer[0];} main () { Channel *c = new BufferedChannel(); } c->send(10); c->send(20); cout << c->receive() << "\n";

Holding Partial Implementation I class Channel { protected: int buffer[10]; int size; public: virtual void send(int data) = 0; virtual int receive () = 0; }; class FIFOChannel: public Channel { public:

Holding Partial Implementation II FIFOChannel () { }; virtual void send(int data); virtual int receive (); }; class LIFOChannel: public Channel { public: LIFOChannel () { }; virtual void send(int data); virtual int receive (); };

Class Member Visibilities Private Committed only Locally Public Committed to External Classes Protected Committed to Subclasses Restricted Committed to a Subset of External Classes Choosing the right visibilities is important for Contracts The right level of encapsulation enforces the abstraction Visibility has impact on refinability

Outline 1 Classes: Implementing Interfaces 2 3 4 5

The Open-Closed Principle: Applying to Classes Never Change an interface of a class once the class is published. The Contract (in our case, the interface) is closed for changes. However, the implementation can be changed. The implementation is open for refinements Unique Ids for component Interfaces

Outline 1 Classes: Implementing Interfaces 2 3 4 5

This: The Self Reference I

This: The Self Reference II #include <iostream> using std::cout; class A { int x; int y; public: A() {} void printthis() { cout << this << "\n";} void f(int p, int q) { x=p; y=q;} void printstate(){cout<<x<<" "<<y<<"\n";} };

This: The Self Reference III main () { } A *a1 = new A(); A *a2 = new A(); cout << a1 << "\n"; a1->printthis(); cout << a2 << "\n"; a2->printthis();

Uses of Self References For sharing method bodies across instances of a class Returning self e.g. cascaded operations Self in parameter passing Dynamic Binding in Inheritance Hierarchies

Self Reference Bindings Is a self reference available in abstract classes? Is a self reference available in class members? Is a self reference available in instance members?

Outline 1 Classes: Implementing Interfaces 2 3 4 5

Inheritance for Conceptually Compatible Classes Contract Conformance (Conceptual Inheritance) Extension Refinement Is Kind Of Relationship Subclass (Derived Class) Superclass (Base Class)