Object-Oriented Programming; what is Inheritance, Polymorphism, Abstraction & Encapsulation?

How to Find Memory Leaks in Your Application
How to Recover Lost Data in Windows 10 in 2020

Object-oriented programming refers to the concept in high-level languages such as Java and Python that uses Objects and classes in their implementations. OOP has four major building blocks which are, Polymorphism, Encapsulation, Abstraction, and Inheritance. There are other programming paradigms such as Procedural programming in which codes are written in sequentially. Python and Java are multi-paradigm high-level programming languages that means they support both OOP and procedural programming. A programmer decides on the paradigm to use based on his expertise and the problems his trying to solve. However, there is no controversy that OOP makes programming easier, faster, more dynamic, and secured. This is a major reason Java and Python are the top most popular programming languages in the world today

Object-Oriented Programming; what is Inheritance, Polymorphism, Abstraction & Encapsulation?, FusionReactor
If you want to learn Java and Python or any other object-oriented programming languages, then you must understand these Object-Oriented Programming paradigms which are a relatively easy concept to understand. Let’s take a look at them.

Object-oriented programming refers to the concept in high-level languages such as Java and Python that uses Objects and classes in their implementations. OOP has four major building blocks: Polymorphism, Encapsulation, Abstraction, and Inheritance. There are other programming paradigms, such as Procedural programming, in which codes are written sequentially. Python and Java are multi-paradigm high-level programming languages that support OOP and procedural programming. A programmer decides on the paradigm to use based on his expertise and the problems his trying to solve. However, there is no controversy that OOP makes programming more accessible, faster, dynamic, and secure. This is why Java and Python are the world’s top most popular programming languages today.

Suppose you want to learn Java, Python, or other object-oriented programming languages. In that case, you must understand these Object-Oriented Programming paradigms, which are relatively straightforward concepts. Let’s take a look at them.

What is Inheritance?

In Java and Python, codes are written in objects or blocks if you adopt the OOP methodology. Objects can interact with one another by using the properties of each block or extending the functionalities of a block through inheritance.  Inheritance ensures that codes are reused. A programmer can use millions of Java and Python libraries through inheritance. The properties of a class can be inherited and extended by other classes or functions. There are two types of classes. One is the Parent or base class, and the other is the child class which can inherit the properties of the parent class. Inheritance is a major pillar in Object-Oriented programming. It is the mechanism by which classes in Java, Python, and other OOP languages inherit the attribute of other classes.

A parent class can share its attributes with a child class. An example of a parent class implementation is in DDL (Dynamic-link library). A DDL can contain different classes used by other programs and functions.

A real-world example of inheritance is a mother and child. The child may inherit height, Voice patterns, and color. The mother can reproduce other children with the same attributes as well.

You can create a function or class called “Move Robot,” which controls a robot to move. And you could create methods and functions in other programs that can inherit the ” Move Robot” Class without rewriting the codes repeatedly.  You can also extend this class by inheriting it and writing a few more codes to it that would instruct a robot to move and also run in some specific circumstances using the if and else statements.  With inheritance, you can create multiple robots that would inherit the attributes of the parent class “Move Robot,” which ensures code reusability.

In summary, Inheritance is concerned with the relationship between classes and methods, which is like a parent and a child. A child can be born with some of the attributes of the parents. Inheritance ensures the reusability of codes just the way multiple children can inherit the attributes of their parents.

When we want to create a function, method, or class, we look for a superclass that contains the code or some of the code we want to implement. We can then derive our class from the existing one. In Java, we do this by using the keyword “Extends,” and in Python, we achieve this by inheriting the attributes of a class by calling up the class name.

Here is an example :

A vehicle class would define fields for speed. All vehicles can travel at some speed (even if 0 when stationary).

All boats would define buoyancy and draft, and then the specific types (sailing, paddle, speed) would define their method of propulsion.

Cars define the type of fuel, engine size, etc.

Airplanes would have logic for flight, i.e., weight limits, etc.

public class Vehicle {
public float speedInKPH;
}
public class Car extends Vehicle {
public int numberOfWheels;
public int numberOfSeats;
}
public class Plane extends Vehicle {
public long maximumTakeoffWeight;
}
public class SportsCar extends Car {
boolean hasSoftTop;
}

What is Polymorphism?

Object-Oriented Programming; what is Inheritance, Polymorphism, Abstraction & Encapsulation?, FusionReactor
Polymorphism means existing in many forms. In Java and Python, variables, functions, and objects can exist in multiple forms. There are two types of polymorphism which are run-time polymorphism and compile-time polymorphism. Run time can take a different form while the application is running, and compile time can take a different form during compilation.

An excellent example of Polymorphism in Object-oriented programing is cursor behavior. Depending on the user’s behavior or program mode, a cursor may take different forms, like an arrow, a line, a cross, or other shapes. With polymorphism, a method or subclass can define its behaviors and attributes while retaining some of the functionality of its parent class. This means you can have a class that displays the date and time, and then you can create a method to inherit the class but it should display a welcome message alongside the date and time. The goal of Polymorphism in Object-oriented programming is to enforce simplicity, making codes more extendable and easily maintaining applications.

Inheritance allows you to create class hierarchies, where a base class gives its behavior and attributes to a derived class. You are then free to modify or extend its functionality. Polymorphism ensures that the proper method will be executed based on the calling object’s type.

Program codes would run differently in a different operating system. The ability of program code exhibiting different behaviors across the operating system is known as polymorphism in OOP. You can create a class called “Move” and then four people create animals that would inherit the move class. But we don’t know the type of animals that they would create. So polymorphism would allow the animals to move but in different forms based on the physical features

A creates a Snail that inherits the move class, but the snail would crawl

B creates a Kangaroo that inherits the move class, but the Kangaroo would leap

C creates a Dog that inherits the move class, but the dogs would walk

D creates a Fish that inherits the move class, but the Fish would swim.

Polymorphism has ensured that these animals are all moving but in different forms. How the programs would behave would not be known until run time.

What is Encapsulation?

This is a programming style where implementation details are hidden. It reduces software development complexity significantly. With Encapsulation, only methods are exposed. The programmer does not have to worry about implementation details but is only concerned with the operations. For example, if a developer wants to use a dynamic link library to display date and time, he does not have to worry about the codes in the date and time class rather he would use the data and time class by using public variables to call it up. In essence, encapsulation is achieved in Python and Java by creating Private variables to define hidden classes and then using public variables to call them up for use. With this approach, a class can be updated or maintained without worrying about the methods using them. If you are calling up a class in ten methods and need to make changes, you don’t have to update the entire methods rather; you update a single class. Once the class is changed, it automatically updates the methods accordingly. Encapsulation also ensures that your data is hidden from external modification. Encapsulation is also known as Data-Hidden.

Encapsulation can be viewed as a shield that protects data from getting accessed by outside code. In object-oriented programming, encapsulation binds data and code as a single unit and enforces modularity.

What is Abstraction?

Abstraction in Java and Python is a programming methodology in which details of the programming codes are hidden away from the user, and only the essential things are displayed to the user. Abstraction is concerned with ideas rather than events.

Its like in our vehicle example some of the vehicles need to be started before they can move.

public interface Startable {
void start();
}
public class Car extends Vehicle implements Startable {
public int numberOfWheels;
public int numberOfSeats;
public void start() {
// some implementation
}
}

You don’t need to care about how to start a car, plane, boat etc, if it implements Startable you know it will have a start method.

Why is Inheritance, Polymorphism, Abstraction & Encapsulation used?

The main idea behind Object Oriented Programming is simplicity, code reusability, extendibility, and security. These are achieved through Encapsulation, abstraction, inheritance, and polymorphism. For a language to be classified as OOP, it must have these 4 OOP blocks.  Abstraction has to do with displaying only the relevant aspect to the user, for example, turning on the radio, but you don’t need to know how the radio works. Abstraction ensures simplicity. Inheritance has to do with methods and functions inheriting the attributes of another class. The main aim is code reuse which ensures that programs are developed faster. DRY (don’t repeat yourself) is a concept in inheritance which implies that in a program, you should not have different codes that are similar. Instead, have one class and use other methods to call them and extend the functionalities where necessary. Polymorphism allows program code to have different meanings or functions while encapsulation is the process of keeping classes private so they cannot be modified by external codes.

Monitoring OOPs FusionReactor?

FusionReactor is an APM (Application Performance Management) tool that monitors and manages applications written in various programming languages, including object-oriented languages such as Java. It provides features for monitoring and optimizing the performance of object-oriented programs, including the ability to

Additionally, FusionReactor provides real-time performance analytics, error detection and analysis, and a powerful reporting system that can help developers to identify and resolve performance issues in their object-oriented programs.

Object-Oriented Programming; what is Inheritance, Polymorphism, Abstraction & Encapsulation?, FusionReactor

FusionReactor will monitor code written in Object-Oriented languages