Download Object-Oriented Programming: CS480 Lecture Notes by Prasad and more Lecture notes Object Oriented Programming in PDF only on Docsity!
CS480 (Prasad) L3OOP 1
Object-Oriented Programming
Programming with Data Types
to enhance reliability and productivity
(through reuse and by facilitating evolution)
CS480 (Prasad) L3OOP 2
- Object (instance)
- State (fields)
- Behavior (methods)
- Identity
- Class
- code describing implementation of an object - Data Abstraction - Modularity - Encapsulation - Inheritance - Polymorphism
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
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.
CS480 (Prasad) L3OOP 5
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 ).
CS480 (Prasad) L3OOP 6
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)
Modularity
⢠Aspect of syntactically grouping related
declarations. (E.g., fields and methods of a data type.)
⢠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
CS480 (Prasad) L3OOP 13
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) );
CS480 (Prasad) L3OOP 14
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 ); }
Open-closed principle
⢠A class is closed because it can be
compiled, stored in a library, and made
available for use by its clients.
⢠A class is open because it can be extended
by adding new features (operations/fields),
or by redefining inherited features.
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
CS480 (Prasad) L3OOP 17
Polymorphism : Example
class Eg {
public static void main (String[] args) {
Rectangle r = new Rectangle(5,6);
System.out.println( r.perimeter() );
r = new ColoredRectangle(Color.red,5,10) ;
System.out.println( r.perimeter() );
Polymorphic Variable r
CS480 (Prasad) L3OOP 18
r
new Rectangle(5,6)
new ColoredRectangle(red,5,10)
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
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]
CS480 (Prasad) L3OOP 25
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) ); } }; CS480 (Prasad) L3OOP 26
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 } };
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
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
CS480 (Prasad) L3OOP 29
Polymorphic Data Structure and Dynamic Binding in C++
void main (char* argv, int argc) { const RSLEN = 3; // coercion, no dynamic binding Rectangle rs [RSLEN]= { Rectangle(5,6), ColoredRectangle(0,1,1), Square(5)} ; for (int i = 0 ; i < RSLEN ; i++ ) cout << rs[i].perimeter() << endl; } void main (char* argv, int argc) { const RSLEN = 3; // polymorphism 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; }
CS480 (Prasad) L3OOP 30
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
Summarizing :Java vs C++ vs C#
As will be seen ā¦
- C# versions combine the syntax of Java and
C++ but support only āreferences to structuresā similarly to Java
- C# version 1 simulates the Java semantics of
polymorphism and dynamic binding by
overriding when the parent and child method
signatures match
- C# version 2 enables avoiding overriding and dynamic binding due to coincidental signature match
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; } }
CS480 (Prasad) L3OOP 37
Reuse : Summary
⢠Inheritance and Polymorphism
- code sharing / reusing implementation
⢠Polymorphism and dynamic binding
- behavior sharing / reusing āhigher-levelā code
- Accommodating variations in implementation at run-time.
⢠Generics / Templates
- Accommodating variations in type
CS480 (Prasad) L3OOP 38
Styles : Procedural vs Object-Oriented
Switch stmt. (Pascalās
Variant record and
Case stmt.)
using switch/case
procs. incremental
and modularity view
- Javaās extends for sub-
class and implements
for sub-type.
using type tags
(data/ops) incremental
too extensible
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
Inter-Class Relationships
āA CarOwner is a Person and has a Car.ā
⢠Composition ( Client Relation ) (ā has a ā)
⢠Inheritance ( Subclass Relation ) (ā is a ā)
class CarOwner extends Person { Car c; ... }
- 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; ... }
CS480 (Prasad) L3OOP 41
Person fields
CarOwner fields
CarOwner fields
Subclass instance ; Client field
Car
Car
CS480 (Prasad) L3OOP 42
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.
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))
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();