top of page

Object Oriented Programming Part 2: Four Principles of OOP

Object Oriented Programming follows four main principles, namely Encapsulation, Abstraction, Inheritance, and Polymorphism. These principles are crucial for understanding how OOP works and how to efficiently design an OOP.


(Source: francescolelli.info/tutorial/object-oriented-programming-a-curated-set-of-resources)

Encapsulation is a concept of restricting access to vital information within an object. These variables and methods are contained within the object such that it is inaccessible from outside the object itself.


This hiding information feature aims to secure internal data and information and avoid data corruption. Specific methods or functions are required for accessing such information without exposing the program structure or other classified data.


Abstraction is similar to encapsulation; However, the goal of abstraction is simplicity. Other objects only interact with relevant attributes and methods and hide unnecessary (often large) of the code. In other words, the class or object works as a black box and other classes are uninformed about how such class or object actually works. Abstraction also restricts impacts of changes of the code; hence, it is easier to debug and modify the code.


Inheritance is a concept of creating a subclass or child class that shares certain attributes or methods. In other words, the program creates a ‘hierarchy’ based on similarities between the parental class and the child class. The goal of this concept is also simplicity and efficiency since creating two almost similar are unnecessary. For example, we want to create objects for car, rail, and airplane.


These objects are different types of vehicles thus we could create a parental class Vehicle containing common attributes for those objects and subclass for each vehicle type containing unique variables for each type.


Polymorphism is a concept of ‘shaping’ one object to behave in a certain way depending on the necessity. Using the previous example, let define a method for moving forward (namely Move_Forward) that is accessible for all objects.


Every vehicle could, in fact, move forward, however, each one operates differently than the others. Therefore, Move_Forward method would execute different steps depending on which object is calling.

1 view0 comments

Comments


bottom of page