Instant Knowledge #1 OOP

OOP(Object Oriented Programming)'s

Encapsulation:

Encapsulation means wrapping up data and information under a single unit. It includes data, and functions which can affect the class.

Example of encapsulation :

In Colleges, there are different sections or departments which keep records of students in their respective departments. Like IT and Electrical department. Now if Someone from the IT department wants to get information about some students then he/she cannot directly access the data.

To do this he/she can ask the head of the department to provide the necessary information regarding the student. So here we can consider the whole college as a class and different departments as the data encapsulated inside the college class.

Abstraction:

Abstraction means providing only the information which we want to make public, the rest of the data is private and not easily accessible. This helps in many scenarios where we don't want to show the internal working of something and want to hide it.

The data which is hidden may or may not be that private for example:

Example of abstraction :

While driving a car, we use an accelerator to increase the speed of the car, but we don't want to know how it's working or how it is increasing the speed of our car.

Another example would be, When including header files in C++ like <stdio.h>. In this header file, printf function is there which prints the data which is present inside the parenthesis. We only want to use this function ignoring how it is working internally.

This is an abstraction in the real world.

Polymorphism:

Polymorphism means having many forms. It helps in code re-usability as a single function can be used in many other classes or within the same class. It allows to write code that can along multiple objects.

Example of polymorphism :

Suppose we want to print the area of a circle and rectangle. So we can create a superclass and define there a function with a name area and can reuse it in a rectangle as well as a circle.

Inheritance:

The ability of a class to derive properties of some other class. Here a parent class is there which shares some of its properties with its child classes. It helps in code reusability, which means less code and more functionality.

Example of inheritance :

Suppose there is a human class and some other classes such as male and female class. Now male and female classes can inherit all the properties present inside the human class.

So here Human class is the parent class which shares its properties and functionality with the male and female classes.