The four pillars of OOPs, abstraction, encapsulation, inheritance, and polymorphism, are integral to understanding and using OOP. By using these four pillars of OOPs, developers can create programs that are powerful, maintainable, and extensible, making them a great choice for software development.
Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic. An object can be defined as a data field that has unique attributes and behavior.
Abstraction:
Abstraction is the process of hiding the internal details of an application from the outer world. Abstraction is used to describe things in simple terms. It’s used to create a boundary between the application and the client programs.
Different types of abstraction. There are primarily two types of abstraction implemented in OOPs. One is data abstraction which pertains to abstracting data entities. The second one is process abstraction which hides the underlying implementation of a process.
functionhitAPI(type){
if
(
type
instanceofInitialLoad)
{
// Implementation example
}
else
if
(
type
instanceofNavBar)
{
// Implementation example
}
else
{
// Implementation example
}
}
Implement Abstraction:
To implement abstraction using abstract classes: We declare a class as an abstract class using abstract keyword. This will work as a parent class for the concrete classes. Inside the abstract class, we declare abstract methods and provide default implementation for the non-abstract methods.
Data Abstraction:
Data abstraction is a process of hiding unwanted or irrelevant details from the end user. It provides a different view and helps in achieving data independence which is used to enhance the security of data. The database systems consist of complicated data structures and relations.
Encapsulation:
Encapsulation is a way to restrict the direct access to some components of an object, so users cannot access state values for all of the variables of a particular object. Encapsulation can be used to hide both data members and data functions or methods associated with an instantiated class or object.
There are three types of encapsulation, namely Member variable encapsulation, Function encapsulation and class encapsulation, each of which has its own significance with respect to member variables of a class, functions, APIs when it comes to the implementation of any specific application possessing encapsulation.
varDog
=(function
()
{
// Private
var
play
=
function
()
{
// play implementation
};
// Private
var
breed
="Dalmatian"
// Public
var
name
="Rex";
// Public
var
makeNoise
=
function
()
{
return
'Bark bark!';
};
return
{
makeNoise:
makeNoise
,
name:
name
};
})();
Member Variable Encapsulation:
All data members should be defined as Private members of the Class in Object-Oriented Programming. Setters and Getters functions should be used by any object that wants to change or retrieve the value of a data member.
Function Encapsulation:
Encapsulation is a way to restrict the direct access to some components of an object, so users cannot access state values for all of the variables of a particular object. Encapsulation can be used to hide both data members and data functions or methods associated with an instantiated class or object.
Class Encapsulation:
In encapsulation, a class’s variables are hidden from other classes and can only be accessed by the methods of the class in which they are found. Encapsulation in Java is an object-oriented procedure of combining the data members and data methods of the class inside the user-defined class.
Inheritance:
Inheritance refers to the assets that an individual bequeaths to their loved ones after they pass away. An inheritance may contain cash, investments such as stocks or bonds, and other assets such as jewelry, automobiles, art, antiques, and real estate.
Single Inheritance:
The inheritance in which a single derived class is inherited from a single base class is known as the Single Inheritance. It is the simplest among all the types of inheritance since it does not include any kind of inheritance combination or different levels of inheritance.
Multiple Inheritance:
Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit features from more than one parent object or parent class.
Multilevel Inheritance:
Multilevel Inheritance in C++ is the process of deriving a class from another derived class. When one class inherits another class it is further inherited by another class. It is known as multi-level inheritance.
Hierarchical Inheritance:
In Hierarchical inheritance, more than one sub-class inherits the property of a single base class. There is one base class and multiple derived classes. Several other classes inherit the derived classes as well. Hierarchical structures thus form a tree-like structure.
Hybrid Inheritance:
Hybrid Inheritance in C++ is the process by which a sub class follows multiple types of inheritance while deriving properties from the base or super class. This is also known as Multipath Inheritance for the same reason.
Polymorphism:
The word polymorphism is derived from Greek and means “having multiple forms.” Apart from computer programming, the idea of polymorphism occurs in other real-world areas, including biology, chemistry and drug development. Polymorphism is one of the most important concepts in OOP.
// Let's set up an Animal and Dog example
functionAnimal(){}
functionDog(){}
Animal.prototype
.makeNoise=
function(){
console
.log("Base noise");
};
// Most animals we code up have 4. This can be overridden if needed
Animal.prototype
.legs
=4;
Dog.prototype
=new
Animal();
Dog.prototype
.makeNoise=
function(){
console
.log("Woof woof");
};
varanimal
=new
Animal();
vardog
=new
Dog();
animal
.makeNoise();// Base noise
dog
.makeNoise();// Woof woof- this was overridden
dog
.legs
;// 4! This was inherited
Compile-time polymorphism:
Compile-time polymorphism is a polymorphism that is resolved during the compilation process. Overloading of methods is called through the reference variable of a class. Compile-time polymorphism is achieved by method overloading and operator overloading.
Run-time polymorphism:
Runtime polymorphism in Java can be carried out only by method overriding. Method overriding occurs when objects possess the same name, arguments, and type as their parent class but have different functionality. When a child class has such a method in it, it is called an overridden method.