Polymorphism, Encapsulation, Data Abstraction and Inheritance in Object-Oriented Programming
Join the DZone community and get the full member experience.
Join For FreeObject-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 code is written sequentially.
Python and Java are multi-paradigm high-level, programming languages. This means they support both OOP and procedural programming. A programmer decides on the paradigm to use based on their expertise and the problems they're trying to solve. However, there is no debate that OOP makes programming easier, faster, more dynamic, and secure. This is a major reason Java and Python are two fo 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. Let’s take a look at them.
You may also like: A Systematic Approach to Write Better Code With OOP Concepts.
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 inherit the attributes 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.
You can create a function or class called “Move Robot," which controls a robot to move. And you could create a method and function in other programs that can inherit the "Move Robot” Class without rewriting the code over and over again. You can also extend this class by inheriting it and writing code 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 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.
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 they 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 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 method. 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 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.
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.
An excellent example of Polymorphism in Object-Oriented Programing is a cursor behavior. A cursor may take different forms like an arrow, a line, a 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 goal of Polymorphism in Object-Oriented Programming is to enforce simplicity, making code more extendable and easily maintainable.
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.
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.
Abstraction
Abstraction in Java and Python is a programming methodology in which details of code are hidden from the user and only essential functionalities are displayed to the user. Abstraction is concerned with ideas rather than events. It’s like a user running a program (Web Browser) without seeing the background code. Abstraction is achieved in either Abstract classes or interfaces in Java and Python. NetBeans and Eclipse IDEs implement abstraction for Java while Django implements abstraction for Python.
A programmer uses an Integrated Development environment to design a UI without worrying about how the IDE generates HTML code. In essence, Abstraction displays the essential details for the user alone.
Further Reading
Published at DZone with permission of nick flewitt. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments