OOP usually combines a group of properties (variables) and methods (functions) into a single unit known as Object which are organized
into class where each individual object can be combined together as a group.
There are four main pillars of OOP as follows:
- Encapsulation
- Inheritance
- Abstraction
- Polymorphism
In a program, the objects of different classes try to commnunicate with each other automatically. If we want our program not to do that we need to encapsulate the objects into individual classes. Through encapsulation, classes cannot communicate or interchange specific properties and methods of an object.
The concept of encapsulation is similar to encapulating a pill or containing the medicine inside its coating but the encapulation in a digital way is used to form a protective coating/barries on a specific piece of information which separates in from the rest of the code but still a programmer can create different objects or reuse an object multiple times throughout the different part of code.
Let’s consider a television example and understand how internal implementation details are hidden from the outside class. Basically, in this example, we are hiding inner code data i.e. circuits from the external world by the cover.
It is a mechanism in which one object can acquire all the properties and behaviours of a parent object. The main idea behind the inheritance is that you can create new classes building upon existing class. When a class is inherited from another class it can access/reuse all the fields and properties of parent class, while having its on specific fields and methods.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
- For instance, in the animal world, an insect would be a superclass. All insects share similar properties, such as having six legs and an exoskeleton. Grasshoppers and ants are both insects and inherited similar properties.
- Another example could be a Shape class, which can have different child classs like Rectangle, Square, Triangle which are shapes having their own dynamics but they can also use the fields of parent class like number of sides, etc.
There are six types of inheritance but their use can be vary according to the programming language you are using for OOP.
- Single Inheritance
- Mutliple Inheritance
- Multilevel Inheritance
- Multipath Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
In this type of inheritance, the derived class can only be created from a parent class.
For example: Class A is the parent class and Class B is the child class since Class B inherits the features and behavior of the parent class A.
This type of inheritance, allows you to inherit a class from more than one base classes. This type of inheritance is not supported by many famous object oriented languages like Java, C#, F and .NET. For Example:
In this type of inheritance, a derived class is being created from other derived class. This type of inheritance more relates to a family tree hierarchy, like there will be the grandparents then parents and then their children and so on. Each class is being drived from the previous class.
In this inheritance, a derived class is created from another derived classes and the same base class of another derived classes. This inheritance is not supported by .NET Languages like C#, F# etc.
For example: We have 4 classes A,B,C,D. Let say Class A is the base class and B,C, and D are being derived from A and also the D class is being derived from B and C too.
In this inheritance, more than one derived classes are created from a single base class and futher child classes act as parent classes for more than one child classes.
For example, class A has two childs class B and class D. Further, class B and class C both are having two childs - class D and E; class F and G respectively.
This is combination of more than one inheritance. Hence, it may be a combination of Multilevel and Multiple inheritance or Hierarchical and Multilevel inheritance or Hierarchical and Multipath inheritance or Hierarchical, Multilevel and Multiple inheritance.
Since .NET Languages like C#, F# etc. does not support multiple and multipath inheritance. Hence hybrid inheritance with a combination of multiple or multipath inheritances is not supported by .NET Languages. For example:
Abstraction is like an extension of encapsulation because it hides certain properties and methods from the outside code to make the interface of the objects simpler. Programmers use abstraction for several beneficial reasons. Overall, abstraction helps isolate the impact of changes made to the code so that if something goes wrong, the change will only affect the variables shown and not the outside code.
Another way to understand this is to consider the human body. The skin acts as an abstraction to hide the internal body parts responsible for bodily functions like digesting and walking.
This technique meaning "many forms or shapes" allows programmers to render multiple elements depending on the type of object. This concept allows programmers to redefine the way something works by changing how it is done or by changing the parts in which it is done. Terms of polymorphism are called overriding and overloading.
To better understand the two terms of polymorphism called overloading and overriding, it helps to visualize the process of walking. Babies learn to crawl first by using their arms and legs. Once they learn to stand and walk, they are ultimately changing the body part used to accomplish the act of walking. This term of polymorphism is called overloading.
To understand the next term of overriding, think of how you naturally walk in the direction you are facing. When you stop and walk backward, this changes the direction of your path and also the mechanism of function. You are overriding the natural action that you usually complete.