What are the Object Oriented Programming Principles
Object Oriented Programming Principles – The principles of object oriented programming are three. These three principles are as follows-
What are three principles of object oriented programming
-
Encapsulation
What is Encapsulation
The combining of data and methods in a single unit is called encapsulation. This single unit is called class.
Data declared inside a class is not accessible directly from outside of the class.
Methods or functions declared inside the class can only access the data members declared in the class.
Encapsulation provides data hiding and prevents accidental use of data from outside of the class.
Example
Class Person
Data members are as follows-
- Person name
- Person id
- Person address
Methods are as follows-
- getdata()
- showdata()
Data members person name, person id and person address can be used and manipulated by the methods getdata() and showdata() only.
Wrapping these data members and methods in a class is called encapsulation.
-
Inheritance
Inheritance is another principle of object oriented programming. It is a very important feature of object oriented programming that provides reusability in addition to better structuring of classes.
What is Inheritance
The mechanism of deriving a new class from an old class is called inheritance. The old class is called a superclass or parent class or base class.
The new class is called subclass or child class or derived class. Inheritance allows adding new features also in the child class and the features of parent class automatically accessed by the child class.
Kinds of Inheritance
There are following kinds of Inheritance-
- Single Level Inheritance
Once superclass and one subclass
- Multilevel Inheritance
In multilevel Inheritance new class is derived from another derived class
- Multiple Inheritance
Under multiple Inheritance new class is derived from two or more classes.
- Hierarchical Inheritance
One class may have two or more derived classes
- Hybrid Inheritance
Combination of two or more types of inheritance is called hybrid Inheritance
-
Polymorphism
What is Polymorphism
Polymorphism means one name and multiple forms.
One function or method behaves differently in different situations.
According to the parameters and class, one method or function works differently.
There are various kinds of polymorphism.
- Compile time polymorphism
Compile time polymorphism further divided into two categories 1. Method overloading 2. Operator overloading
- Run time polymorphism
Run time polymorphism is implemented via virtual functions or overriding methods.
What are the 4 principles of object oriented programming
Besides above mentioned three principles of object oriented programming one more principle is also considered that is Abstraction.
Therefore four principles of object oriented programming are the following-
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
Abstraction
What is Abstraction
Abstraction means the act of representing essential features without including the background information. Classes use the concept of abstraction.
Classes defined as a list of abstract attributes such as roll number, first name, second name, age, address, etc. and functions or methods that operate on these attributes.
Class encapsulates all the essential properties of the objects.
What are the 4 pillars of OOP
Four pillars of abstraction of oops are
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
Therefore, Object Oriented Programming Principles are three Encapsulation, Inheritance, and Polymorphism, but sometimes Abstraction is also considered the fourth principle of object oriented programming.
– Object Oriented Programming Principles