What is Polymorphism, Encapsulation, Data Abstraction and Inheritance in Object-Oriented Programming?

fusion_reactor

FusionReactor

Posted on June 5, 2020

What is Polymorphism, Encapsulation, Data Abstraction and Inheritance in Object-Oriented Programming?

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

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.

What is Inheritance?

In Java and Python, codes are written in objects or blocks if you are adopting 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. There are millions of Java and Python libraries that a programmer can use 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 inherits 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 that can be used by other programs and functions

A real-world example of inheritance is a mother and child. The child may inherit attributes such as height, Voice patters, 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 method and functions in other programs that can inherit the ” Move Robot” Class without rewriting the codes over and over again.  You can also extend this class by inheriting it and writing few more codes to it that would instruct a robot to move and also run in some specific circumstances using if and else statement.  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 method, which is like a parent and a child. A child can be born with some of the attributes of the parents. Inheritance ensures 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 are capable of traveling 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 its method of propulsion.

Cars define 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 Encapsulation?

This is a programming style where implementation details are hidden. It reduces software development complexity greatly. 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 simply 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 in 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 you 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 essence, Encapsulation binds data and code as a single unit and enforces modularity.

What is Polymorphism

Polymorphism means existing in many forms. Variables, functions, and objects can exist in multiple forms in Java and Python. 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 a cursor behavior. A cursor may take different forms like an arrow, a line, cross, or other shapes depending on the behavior of the user or the program mode. 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 date and time, and then you can create a method to inherit the class but should display a welcome message alongside the date and time. The goals 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 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.

Polymorphism, Encapsulation, Data Abstraction and Inheritance in Object-Oriented Programming

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 imply 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 meaning or functions while encapsulation is the process of keeping classes private so they cannot be modified by external codes.

This article was originally posted in the FusionReactor blog where you will find many interesting articles written by our developer team.

What is FusionReactor?

FusionReactor is an APM for Java applications that features low-level capabilities including profilers, automated root cause analysis, and production debugger.

💖 💪 🙅 🚩
fusion_reactor
FusionReactor

Posted on June 5, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related