CS3180 (Prasad)L3OOP1 Object-Oriented Programming Programming with Data Types to enhance reliability and productivity ( through reuse and by facilitating.

Slides:



Advertisements
Similar presentations
Type Systems and Object- Oriented Programming (III) John C. Mitchell Stanford University.
Advertisements

Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Stéphane Ducasse6.1 Essential Concepts Why OO? What is OO? What are the benefits? What are the KEY concepts? Basis for all the lectures.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Lecture 9 Concepts of Programming Languages
Programming Paradigms cs784(Prasad)L5Pdm1. Programming Paradigm A way of conceptualizing what it means to perform computation and how tasks to be carried.
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
CEG860 (Prasad)L3EG1 Object-Oriented Programming Examples.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Inheritance and Polymorphism CS351 – Programming Paradigms.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object-oriented programming and software development Lecture 1.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Ceg860 (Prasad)L6MR1 Modularity Extendibility Reusability.
Programming Paradigms Procedural Functional Logic Object-Oriented.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Programming Languages and Paradigms Object-Oriented Programming.
OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Chapter 18 Object Database Management Systems. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Motivation for object.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Design.ppt1 Top-down designs: 1. Define the Problem IPO 2. Identify tasks, Modularize 3. Use structure chart 4. Pseudocode for Mainline 5. Construct pseudocode.
Chapter 10 Inheritance and Polymorphism
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object-Oriented Programming Chapter Chapter
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
Object Oriented Programming
ISBN Object-Oriented Programming Chapter Chapter
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
Ceg860 (Prasad)L112Inh1 Inheritance Reusability Extendibility Type Safety Reusability ; Extendibility ; Type Safety (Minimize Repetition, Accommodate Variation.
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 18 Object Database Management Systems. Outline Motivation for object database management Object-oriented principles Architectures for object database.
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
CS 3180 (Prasad)L67Classes1 Classes and Interfaces Inheritance Polymorphism.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
ISBN Chapter 12 Support for Object-Oriented Programming.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Type Checking and Type Inference
Programming Paradigms
Inheritance and Polymorphism
Lecture 9 Concepts of Programming Languages
More Object-Oriented Programming
Programming Paradigms
C++ Object Oriented 1.
Computer Science II for Majors
Lecture 9 Concepts of Programming Languages
Presentation transcript:

CS3180 (Prasad)L3OOP1 Object-Oriented Programming Programming with Data Types to enhance reliability and productivity ( through reuse and by facilitating evolution )

CS3180 (Prasad)L3OOP2 Object (instance) –State (fields) –Behavior (methods) –Identity Class –code describing implementation of an object Data Abstraction Modularity Encapsulation Inheritance Polymorphism

CS3180 (Prasad)L3OOP3 Abstraction General: Focus on the meaning Suppress irrelevant “implementation” details Programming Languages : – Assign names to recurring patterns Value : constant identifier Expression : function Statements : procedure Control : loop, switch Value/ops : interface

CS3180 (Prasad)L3OOP4 Data Abstraction Focus on the meaning of the operations (behavior), to avoid over-specification. The representation details are confined to only a small set of procedures that create and manipulate data, and all other access is indirectly via only these procedures. –Facilitates code evolution.

CS3180 (Prasad)L3OOP5 Data Abstraction : Motivation Client/user perspective (Representation Independence) – Interested in what a program does, not how. – Minimize irrelevant details for clarity. Server/implementer perspective (Information Hiding) – Restrict users from making unwarranted assumptions about the implementation. – Reserve right to change representation to improve performance, … (maintaining behavior).

CS3180 (Prasad)L3OOP6 Data Abstraction : Examples Queues ( empty, enQueue, deQueue, isEmpty ) –array-based implementation –linked-list based implementation Tables ( empty, insert, lookUp, delete, isEmpty ) –Sorted array (logarithmic search) –Hash-tables (ideal: constant time search) –AVL trees (height-balanced) –B-Trees (optimized for secondary storage)

CS3180 (Prasad)L3OOP7 Modularity Aspect of syntactically grouping related declarations. (E.g., fields and methods of a data type.) Package/class in Java. In OOPLs, a class serves as the basic unit for decomposition and modification. It can be separately compiled.

Criteria for Modular Design Supports decomposition for division of labor Supports composition for reuse Supports continuity (incremental updates) for extendibility and smoother evolution Supports understandability Supports protection and isolation CS3180 (Prasad)L3OOP8

ENCAPSULATION Procedural LanguagesObject-based Languages SHARED DATA BET. P & Q proc p;proc q; proc r; /* r can “illegally” access shared data */ Shared Data proc p; proc q; proc r; CS3180 (Prasad)L3OOP9

CS3180 (Prasad)L3OOP10 Controlling visibility of names. Enables enforcing data abstraction Conventions are no substitute for enforced constraints. Enables mechanical detection of typos that manifest as “illegal” accesses. (Cf. problem with global variables) Encapsulation

CS3180 (Prasad)L3OOP11 Data Abstraction : Summary Abstract data typesTheory : Abstract data types Practice : –Information hiding –Information hiding (“server”) –Representation independence –Representation independence (“client”) Language : –Modularity –Encapsulation

CS3180 (Prasad)L3OOP12 Inheritance : Subclasses Code reuse derive Colored-Window from Window (also adds fields/methods) Specialization: Customization derive bounded-stack from stack (by overriding/redefining push) Generalization: Factoring Commonality – code sharing to minimize duplication – update consistency

CS3180 (Prasad)L3OOP13 Inheritance/Redefinition : Example import java.awt.Color; class Rectangle { int w, h; Rectangle (int ww, int hh) { w = ww; h = hh; } int perimeter () { return ( 2*(w + h) ); }

CS3180 (Prasad)L3OOP14 class ColoredRectangle extends Rectangle { Color c; // inheritance ColoredRectangle (Color cc, int w, int h) { super(w,h); c = cc; } } class Square extends Rectangle { Square(int w) { super(w,w); } int perimeter () { // overriding return ( 4*w ); } }

CS3180 (Prasad)L3OOP15 Open-closed principle closedA class is closed because it can be compiled, stored in a library, and made available for use by its clients. Stability openA class is open because it can be extended by adding new features (operations/fields), or by redefining inherited features. Change

CS3180 (Prasad)L3OOP16 Polymorphism (many forms) Integrating objects that exhibit a common behavior and share code. Unifying heterogeneous data. –E.g., moving, resizing, minimizing, closing, etc windows and colored windows

CS3180 (Prasad)L3OOP17 Polymorphism : Example class Eg { public static void main (String[] args) { r Rectangle r = new Rectangle(5,6); r.() System.out.println( r.perimeter() ); r r = new ColoredRectangle (Color.red,5,10) ; r.() System.out.println( r.perimeter() ); }

Polymorphic Variable r CS3180 (Prasad)L3OOP18 r new Rectangle(5,6) new ColoredRectangle(red,5,10)

CS3180 (Prasad)L3OOP19 Signature Signature of a procedure is the sequence of types of formal parameters and the result of a function. Signature of a function also includes its return type. + : real x real  real push : int x stack  stack isEmpty : stack  boolean 0 : int

CS3180 (Prasad)L3OOP20 Overloading Same name for conceptually related but different operations. –E.g., print(5); print(“abc”); print(Table); –E.g., 1 + 2, “abc” + “…” + “xyz” Ambiguity resolved on the basis of contextual information ( signature) –Scalar Multiplication: 2 * [1,2,3] = [2,4,6] –Dot-product: [1,2] * [1,2] = 5 –Cross-product: [1,2,0] * [1,2,0] = [0, 0, 0]

CS3180 (Prasad)L3OOP21 Binding Associating a method call with the method code to run –Resolving ambiguity in the context of overloaded methods –Choices for binding time Static: Compile-time Dynamic : Run-time

CS3180 (Prasad)L3OOP22 Binding: Static vs Dynamic Static binding (resolved at compile-time) Vector. mul(Number) Vector. mul(Vector) –both mul defined in one class Dynamic binding (resolved at run-time) (Array) Stack. push(5) (List) Stack. push(5) –the two pushes defined in different classes

CS3180 (Prasad)L3OOP23 Polymorphism and Dynamic Binding Integrating objects that share the same behavior/interface but are implemented differently. –Representation independence of clients. (Sharing/Reuse of “high-level” code.) –E.g., searching for an identifier in an array of tables, where each table can potentially have a different implementation. –E.g., pushing a value on a stack or a bounded stack.

CS3180 (Prasad)L3OOP24 Dynamic Binding : Example class Eg { public static void main (String[] args) { rs Rectangle [] rs = { new Rectangle(5,6), new ColoredRectangle (Color.red,1,1), new Square(3)} ; iii for (int i = 0 ; i < rs.length ; i++ ) rs[i].() System.out.println( rs[i].perimeter() ); }} Polymorphic data-structure Polymorphic variable Dynamic Binding

CS3180 (Prasad)L3OOP25 Rendition in C++ #include using namespace std; class Rectangle { protected: int w, h; public: Rectangle (int ww, int hh) { w = ww; h = hh; } virtual int perimeter () { return ( 2*(w + h) ); } };

CS3180 (Prasad)L3OOP26 class ColoredRectangle : public Rectangle { private: // inheritance int c; public: ColoredRectangle (int cc, int w, int h) : Rectangle(w,h) { c = cc; } }; class Square : public Rectangle { public: Square(int w) : Rectangle(w,w) {} int perimeter () { // overriding return ( 4*w ); // protected, not private } };

CS3180 (Prasad)L3OOP27 void main (char* argv, int argc) { Rectangle r (5,6); cout << r.perimeter() << endl; ColoredRectangle cr (0,1,1) ; r = cr; // coercion (truncation) cout << r.perimeter() << endl << cr.perimeter() << endl; // inheritance Square s = Square(5); r = s; // NOT polymorphism cout << r.perimeter() << endl; cout << s.perimeter() << endl; // static binding }

CS3180 (Prasad)L3OOP28 void main (char* argv, int argc) { Rectangle* r = new Rectangle(5,6); cout << r  perimeter() << endl; r = new ColoredRectangle(0,1,1) ; cout << r  perimeter() << endl; r = new Square(5) ; cout << r  perimeter() << endl; // polymorphism and dynamic binding // perimeter() explicitly declared virtual }

CS3180 (Prasad)L3OOP29 Polymorphic Data Structure and Dynamic Binding in C++ void main (char* argv, int argc) { const RSLEN = 3; // coercion (homogeneous data) Rectangle rs [RSLEN]= { Rectangle(5,6), ColoredRectangle(0,1,1), Square(5)} ; for (int i = 0 ; i < RSLEN ; i++ ) cout << rs[i].perimeter() << endl; // static binding } void main (char* argv, int argc) { const RSLEN = 3; // polymorphism (heterogeneous data) Rectangle* rs [RSLEN] = { new Rectangle(5,6), new ColoredRectangle(0,1,1), new Square(5)} ; for (int i = 0 ; i < RSLEN ; i++ ) cout << rs[i]  perimeter() << endl; // dynamic binding }

CS3180 (Prasad)L3OOP30 Summarizing :Java vs C++ vs C# Java version uses “references to structures” –Employs polymorphism and dynamic binding C++ version 1, which resembles Java version, uses “structures” –Employs coercion and static binding C++ version 2, which differs from Java version on the surface but simulates Java semantics using “references to structures” –Employs polymorphism and dynamic binding

CS3180 (Prasad)L3OOP31 Summarizing :Java vs C++ vs C# As will be seen … C# versions combine the syntax of Java and C++ but supports only “references to structures” similarly to Java –C# version 1 simulates the Java semantics of polymorphism and dynamic binding by overriding when the parent method’s and the child method’s signatures match –C# version 2 enables avoiding overriding and dynamic binding due to coincidental signature match

CS3180 (Prasad)L3OOP32 Rendition in C# using System.Drawing; class Rectangle { protected int w, h; public Rectangle (int ww, int hh) { w = ww; h = hh; } public virtual int perimeter () { System.Console.WriteLine( "Rectangle.perimeter() called" ); return ( 2*(w + h) ); } } class ColoredRectangle : Rectangle { protected Color c; // inheritance public ColoredRectangle (Color cc, int w, int h):base(w,h) { c = cc; } }

CS3180 (Prasad)L3OOP33 class Square : Rectangle { public Square(int w): base(w,w) { } public override int perimeter () { // overriding System.Console.WriteLine( "Square.perimeter() called" ); return ( 4*w ); } } class EgA { public static void Main (string[] args) { Rectangle [] rs = { new Rectangle(5,6), new ColoredRectangle(Color.Red,1,1), new Square(2) } ; for (int i = 0 ; i < rs.Length ; i++ ) System.Console.WriteLine( rs[i].perimeter() ); } }

CS3180 (Prasad)L3OOP34 Rendition in C# using System.Drawing; class Rectangle { protected int w, h; public Rectangle (int ww, int hh) { w = ww; h = hh; } public int perimeter () { System.Console.WriteLine( "Rectangle.perimeter() called" ); return ( 2*(w + h) ); } } class ColoredRectangle : Rectangle { protected Color c; // inheritance public ColoredRectangle (Color cc, int w, int h):base(w,h) { c = cc; } }

CS3180 (Prasad)L3OOP35 class Square : Rectangle { public Square(int w): base(w,w) { } public new int perimeter () { // unrelated System.Console.WriteLine( "Square.perimeter() called" ); return ( 4*w ); } } class EgA { public static void Main (string[] args) { Rectangle [] rs = { new Rectangle(5,6), new ColoredRectangle(Color.Red,1,1), new Square(2) } ; for (int i = 0 ; i < rs.Length ; i++ ) System.Console.WriteLine( rs[i].perimeter() ); } }

CS3180 (Prasad)L3OOP36 Polymorphism and Dynamic Binding : Examples Viewing various types of files. *.ps, *.ppt, *.html, *.java, etc Determining values of different kinds of expressions. variable, plus-expr, conditional-expr, etc Moving/copying file/directory. Using same bay (interface) to house CD or floppy drive in a laptop. different device drivers Car’s “interface” to a driver.

CS3180 (Prasad)L3OOP37 Reuse : Summary InheritancePolymorphismInheritance and Polymorphism code sharing / reusing implementation Polymorphism and dynamic bindingPolymorphism and dynamic binding behavior sharing / reusing “higher-level” code Accommodating variations in implementation at run-time. Generics / Templates Accommodating variations in type

CS3180 (Prasad)L3OOP38 Styles : Procedural vs Object-Oriented C’s Union type and Switch stmt. (Pascal’s Variant record and Case stmt.) Explicit dispatching using switch/case Addition of new procs. incremental Not ideal from reuse and modularity view Java’s extends for sub- class and implements for sub-type. Automatic dispatching using type tags Addition of new impl. (data/ops) incremental Classes in binary form too extensible

CS3180 (Prasad)L3OOP39 Styles: Procedural vs Object-Oriented Data 1 Data 2 Data 3 Data 2 Data 1 Data 3 Proc 1 Proc 2 Proc 3 Proc 1 Proc 2 Proc 3 UNION PROCEDURE CLASS

CS3180 (Prasad)L3OOP40 Inter-Class Relationships is ahas a “A CarOwner is a Person and has a Car.” has aComposition (Client Relation) (“has a”) is aInheritance (Subclass Relation) (“is a”) class CarOwner extends Person { Car c;... } when the “is” view is legitimate, one can always take the “has” view instead.The difficulty in choosing between the two relations stems from the fact that when the “is” view is legitimate, one can always take the “has” view instead. class CarOwner { Car c; Person p;... }

CS3180 (Prasad)L3OOP41 Person fields CarOwner: Person fields Subclass instance ; Client field Car field

CS3180 (Prasad)L3OOP42 Example : OOP Style vs Procedural Style  Client Determine the number of elements in a collection.  Suppliers Collections : Vector, String, List, Set, Array, etc  Procedural Style A client is responsible for invoking appropriate supplier function for determining the size.  OOP Style Suppliers are responsible for conforming to the standard interface required for exporting the size functionality to a client.

CS3180 (Prasad)L3OOP43 Client in Scheme (define (size C) (cond ( (vector? C) (vector-length C) ) ( (pair? C) (length C) ) ( (string? C) (string-length C) ) ( else “ size not supported ”) ) )) (size (vector 1 2 (+ 1 2))) (size ‘(one “two” 3))

CS3180 (Prasad)L3OOP44 Suppliers and Client in Java interface Collection { int size(); } class myVector extends Vector implements Collection { } class myString extends String implements Collection { public int size() { return length();} } class myArray implements Collection { int[] array; public int size() {return array.length;} } Collection c = new myVector(); c.size();