Towards Object Oriented Computing

Lectures 9-11

enum kind {circle, square, triangle, ...} //enumeration

Shape {

kind S_type; // type field for appropriate operations

Point Centre;

. . .

. . .

// . . . Constructor etc.

// . . . Operations : abstraction for all types of shapes

void draw ();

void rotate(int angle);

// ... Others

}

void draw(){ //pseudo-code

switch(S_type){

case circle:

draw_Circle(); break;

case square:

draw_Square(); break;

case triangle:

draw_Triangle(); break;

case default: break;

} }

We need access to the complete source code and there is possibility of bug introduction while touching the sources.

None of the above are correct way.

        e.g.

Structural  (Y/N/P/X)

Behavior (Y/N/P/X)

Mammals : Man, Animal

Y

X

Parents : Sibling

Y

X

Shape:Circle, Square . .

N

Y

int angle;

vector <shape> vec;                         // pseudo-code

for(i=0; i<vec.size; i++) vec[i].draw();

for(i=0; i<vec.size; i++)vec[i].rotate(angle);

        In the above code, behaviour or abstraction of the shapes, is reused for extendibility. The structural property do not participate much in maintainability of the code. Common structural property make the implementation easier. Anyways the implementation details specific to the structure and encapsulated.

Make a hierarchy of

Thus from both the scenarios we can conclude that start point of software design is commonality of abstraction.

Animal oA;                                 // Animal Ref.

Man oMan = new Man();

Monkey oMnk = new Monkey();

oA = oMan;                                 // Type substitution

oA.climb();                                 // Static: climb like Animal

// Dynamic: climb like Man

oA = oMnk;                                 // Type substitution

oA.climb();                                 // Static: climb like Animal

// Dynamic: climb like Monkey

oMnk = (Monkey) oA;                 // Dynamic Cast : YES

oMan = (Man) oA;                // Dynamic Cast : Exception @ runtime

oMan = (Monkey) oA;                         // Compilation Err’

// No type casting amongst siblings