What is Polymorphism, Encapsulation, Data Abstraction and Inheritance in Object-Oriented Programming?
Tags: polymorphism, encapsulation, abstraction, oop
Published: 2020-06-05 14:46:08

<p>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.&nbsp; 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</p>



<figure class="wp-block-image size-large"><img src="https://www.fusion-reactor.com/wp-content/uploads/2020/06/oop.png" alt="" class="wp-image-44468" srcset="https://www.fusion-reactor.com/wp-content/uploads/2020/06/oop.png 557w, https://www.fusion-reactor.com/wp-content/uploads/2020/06/oop-300x231.png 300w" sizes="(max-width: 557px) 100vw, 557px" /></figure>



<p>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.</p>



<h2><strong>What is Inheritance?</strong></h2>



<p>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.&nbsp; 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</p>



<p>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</p>



<p>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</p>



<p>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.&nbsp; 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. &nbsp;With inheritance, you can create multiple robots that would inherit the attributes of the parent class “Move Robot,” which ensures code reusability.</p>



<p>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.</p>



<p>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.</p>



<p>Here is an example : </p>



<figure class="wp-block-image size-large"><img src="https://www.fusion-reactor.com/wp-content/uploads/2020/06/outfile-2.png" alt="" class="wp-image-44484" srcset="https://www.fusion-reactor.com/wp-content/uploads/2020/06/outfile-2.png 900w, https://www.fusion-reactor.com/wp-content/uploads/2020/06/outfile-2-300x67.png 300w, https://www.fusion-reactor.com/wp-content/uploads/2020/06/outfile-2-768x171.png 768w" sizes="(max-width: 900px) 100vw, 900px" /></figure>



<p>A vehicle class would define fields for speed.  All vehicles are capable of traveling at some speed (even if 0 when stationary).</p>



<p>All boats would define buoyancy and draft and then the specific types (sailing, paddle, speed) would define its method of propulsion. </p>



<p>Cars define type of fuel, engine size etc.</p>



<p>Airplanes would have logic for flight, i.e.  weight limits etc.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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;
}</pre>



<h2>What is Encapsulation?</h2>



<p>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.</p>



<p>Encapsulation can be viewed as a shield that protects data from getting accessed by outside code.</p>



<p>In essence, Encapsulation binds data and code as a single unit and enforces modularity.</p>



<h2>What is Polymorphism</h2>



<figure class="wp-block-image size-large"><img src="https://www.fusion-reactor.com/wp-content/uploads/2020/06/oop2.png" alt="" class="wp-image-44469" srcset="https://www.fusion-reactor.com/wp-content/uploads/2020/06/oop2.png 503w, https://www.fusion-reactor.com/wp-content/uploads/2020/06/oop2-300x187.png 300w" sizes="(max-width: 503px) 100vw, 503px" /></figure>



<p>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.</p>



<p>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.</p>



<p>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.</p>



<p>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.&nbsp; 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</p>



<p>A creates a Snail that inherits the move class, but the snail would crawl</p>



<p>B creates a Kangaroo that inherits the move class, but the Kangaroo would leap</p>



<p>C creates a Dog that inherits the move class, but the dogs would walk</p>



<p>D creates a Fish that inherits the move class, but the Fish would swim.</p>



<p>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.</p>



<h2>What is Abstraction?</h2>



<p>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. </p>



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



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">public interface Startable {
    void start();
}
public class Car extends Vehicle implements Startable {
    public int numberOfWheels;
    public int numberOfSeats;
    public void start() {
       // some implementation
    }
}</pre>



<p>You don&#8217;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.</p>



<h2><strong>Polymorphism, Encapsulation, Data Abstraction and Inheritance in Object-Oriented Programming</strong></h2>



<p>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.</p>

This article was originally posted <a href="https://www.fusion-reactor.com/blog/technical-blogs/polymorphism-encapsulation-data-abstraction-and-inheritance-in-object-oriented-programming/">in the FusionReactor blog</a> where you will find many interesting articles written by our developer team.


<h3>What is FusionReactor?</h3>



<p><a href="https://fusion-reactor.com">FusionReactor is an APM</a> for Java applications that features low-level <a href="/ultimate-edition/">capabilities </a>including profilers, automated root cause analysis, and production debugger. </p>