Docsity

Class Compatibility - Object Oriented Programming - Lecture Slides, Slides of Object Oriented Programming

Partial preview of the text

Download Class Compatibility - Object Oriented Programming - Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Object-Oriented Programming

(OOP)

Lecture No. 6

Class Compatibility

  • A class is behaviorally compatible with another if it supports all the operations of the other class
  • Such a class is called subtype
  • A class can be replaced by its subtype

Example – Class Compatibility

Shape color vertices move setColor draw

Circle radius draw computeArea

Line length draw getLength

Triangle angle draw computeArea

Example – Class Compatibility

File size … open print …

ASCII File … print …

PDF File … print …

PS File … print …

Polymorphism in OO Model

  • In OO model, polymorphism means that different objects can behave in different ways for the same message (stimulus)
  • Consequently, sender of a message does not need to know exact class of the receiver

Example – Polymorphism

Shape

Line Circle Triangle draw

draw

draw draw

draw View

Polymorphism – Advantages

  • Messages can be interpreted in different ways depending upon the receiver class

Shape

Line Circle Triangle draw

draw

draw draw

draw View

Polymorphism – Advantages

  • New classes can be added without changing the existing model

Square draw

Shape

Line Circle Triangle draw

draw

draw draw

draw View