OOP

The four principles of object-oriented programming

Story Highlights
  • The four pillars of object-oriented programming
  • The four principles of object-oriented programming
  • The 4 basics of object-oriented programming
  • The building blocks of object-oriented programming

The four pillars or the four principles or the 4 basics or the building blocks of object-oriented programming are:

1. Abstraction

2. Encapsulation

3. Inheritance

4. Polymorphism

1. Abstraction

Hiding internal details and showing functionality is known as abstraction.

Examples:
(a) Phone call, we don’t know the internal processing.
(b) Consider a real-life example of a man driving a car. The man only knows that pressing the accelerators will increase the car speed or applying brakes will stop the car, but he does not know how on pressing the accelerator, the speed is actually increasing. He does not know about the inner mechanism of the car or the implementation of the accelerators, brakes etc. in the car. This is what abstraction is.

2. Encapsulation

The definition of encapsulation is “the action of enclosing something in or as if in a capsule”.


The meaning of Encapsulation, is to make sure that “sensitive” data is hidden from users. Encapsulation can be achieves by:

  • declare class variables/attributes as private
  • provide public get and set methods to access and update the value of a private variable

When one object acquires all the properties and behaviors of a parent object, it is known as inheritance. It provides code re-usability. It is used to achieve runtime polymorphism.

3. Inheritance

Inheritance is the process of creating a new Class, called the Derived Class, from the existing class, called the Base Class.

The “inheritance concept” can be grouped into two categories:

  • derived class (child) – the class that inherits from another class
  • base class (parent) – the class being inherited from
Base Class and Derived Class

Different Types of Inheritance

Object-Oriented Programming (OOP) support the six different types of inheritance as given below:

  1. Single Inheritance
  2. Multi-level Inheritance
  3. Multiple Inheritance
  4. Multi-path Inheritance
  5. Hierarchical Inheritance
  6. Hybrid Inheritance
Different types of inheritance

4. Polymorphism

Polymorphism means “many forms”, and it occurs when we have many classes that are related to each other by inheritance.

If one task is performed in different ways, it is known as polymorphism.

Examples:

  1. To convince the customer differently
  2. To draw something, for example, shape, triangle, rectangle, etc.
  3. To speak something; for example, a cat speaks meow, dog barks woof, etc.

Generally, method overloading and method overriding are used to achieve polymorphism.

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button
History Of 1st July Get ready for the ICC Men’s Cricket World Cup 2023! Yoga according to the Bhagavad Gita