Sunday, August 21, 2022

Understanding Object Oriented Programming (OOP) with examples

OOP has four pillars
  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism
Abstraction - Hiding design details
Abstraction deals with the design implications. It hides irrelevant information.
Let's use the ubiquitous  car example as below.
A car has many related information.
Make => Maruti, TATA, BMW  and many
Model => Sedan, SUV, Cruiser and many
Paint color => Many
Year of manufacture
Fuel tank capacity
Number of cylinders
License plate 
FastTrack Tag

A parking lot application may be interested in  only license plate.
A Toll gate application may be interested in  only Fastrack tag
A Car dealer application may be interested in all but last two. 
This is called abstraction where unneeded details are hidden.

Encapsulation - Hiding implementation details
Encapsulation deals with the implementation implications. It hides implementation details.
For example, Consider a class Circle that implements Circle geometry shape which can be filled with a color. The color information can be supplied in many formats  
  • Readable strings -  "red", "blue"
  • RGB color integers  0xFF0000, 0x0000FF
However internal storage details of the color in the shape class  is irrelevant to the client application that uses this class. For example, the shape class might store it as a number or  a string.
This is encapsulation where implementation details are hidden.

Inheritance -  reuse


Inheritance deals with the reuse of the code and data of a class. It is achieved by class derivation.
For example let's consider a class that simulates ATM Machine in the bank. The current implementation supports services via touch pad. Let's say there is a new requirement to support a touchless system where a customer can avail services using a smartphone app connecting to the ATM machine via Bluetooth.
Updating existing ATM class to support this could be messy. Creating new ATM class from scratch  to support touchless and touch pad could be over burden. A right balance can be struck by deriving or inheriting the new ATM class from existing ATM class. This way, all the existing functionality touchpad services will be available to the new ATM class for free. The new ATM class will only have to implement services for the new touchless system.
This is inheritance where existing functionality of a class is reused  in a derived class that implements new functionality.

Polymorphism - Override at runtime


Polymorphism means overriding a base class method at runtime by the derived class. 
For example let's consider a digital drawing application such as AutoCAD. The application allows drawing different geometric shapes such as square, circles, polygons etc. Let's say the application stores all geometric shapes in a list and draws them all  on repaint.
From programming perspective, all the geometric shapes can be derived from one base class having a method called draw(). All the geometric shapes override with its own draw() method to draw the shape.
So the list of geometric shapes will be a container of  Shape types holding derived shape objects such as circle, triangle, rectangle etc. On repaint, the paint application simply calls the draw method of each  element in the list which in turn will call individual geometric shapes draw method. C++ manages this using v tables.
This is polymorphism where the overriding method of derived class is executed when the same method  from the base class is called.

No comments:

Post a Comment