The movement towards the Object-Oriented Approach In programming languages

Blog
Spread the love

As we are all aware the object-oriented programming approach dominates these days to a high extent. Several programming languages such as Java, JavaScript, Python etc. are enriched with this approach.

It is a better replacement for the procedural programming approach which earlier followed most of the time. It is also known as Modular Programming. In it, functions have statements embraced inside curly braces. Each of these functions performs a subtask. C language and Pascal are two commonly Procedural programming languages.

Some differences between both approaches are given below.

object-Oriented

So here we have a look at the OOPs approach that makes java so popular and robust. As it is a programming approach which makes the developer’s life very easy.

There is 3 major concept, which makes a language object-oriented and those are P, I, and E [Polymorphism, Inheritance, and Encapsulation].

opps

Some other OOPs features are Data Hiding, Data Abstraction and many more also discussed below.

Why OOPs

Problem 1-(Code Length)

Earlier when we write a program or instructions with a procedural programming approach, the code became very lengthy. This problem was solved by introducing the reusability of code in Java with the help of the Inheritance concept.

Problem 2-(Security)

Security of data was a serious concern, as stealing data from program code or memory creates security breaches. This problem is also solved by an abstraction which is the OOPs concept.

Problem 3-(Relation with real world)

It’s very hard to relate PPA to real-world Objects/Entities. In the real world, everything is treated as an object and every object has some property and behaviour. (For example, if we consider a car as an object then the car object has some property and behaviour).

real-world-objects

As in the real world, there is a tight coupling in the properties and behaviours of any object.

So those were major concerns, which cause to evolve the OOPs’ concept/approach. Now have a look at major OOPs concepts.

Polymorphism

Whenever any object represents different behaviour in different situations/behaviour, this behaviour of any object is known as polymorphism. It reduces the complexity of programming by simply providing method/function overriding/overloading.

The best example of polymorphism is the method println() in java, which can be used to print any type of data. This is possible because many println() methods are overridden for printing different data(as shown below). Due to this concept complexity of methods is reduced to a great extent.

polymorphism

It is of two types, one is Runtime polymorphism and another is Compile time polymorphism.

Runtime Polymorphism- It can be achieved by method over-riding. (When a method with the same signature is used in another child class but with a change in implementation)

Method Overriding- Any child class can provide its custom implementation of any instance method, instance property or subscript that it would inherit from a parent class. It is known as Overriding.

Compile time Polymorphism- It can be achieved by method overloading. (When a method with the same name, but different arguments is used in another class)

Encapsulation

A class can be seen as a capsule, in which we have to store the property and behaviour of an object (Just like medicine wrapped in a capsule to protect it). As any real-world object has some properties and behaviour that are tightly bound with that.

So the binding of properties and behaviour of any object in a place is known as Encapsulation. In java, to wrap properties (Data members) and behaviour (Methods/Functions) we have to put both into a class. Class is a keyword, used to represent the object. As we can see below-

encapsulation
advantages-of-encapsulation

The following are some of the advantages of encapsulation:

Data Hiding: In this case, the user will be unaware of the class’s internal implementation. Even the user will have no idea how the class stores data in variables. Users will only be aware that the values are sent to a setter method and that variables are initialized with that value.

It also promotes reusability and makes it simple to alter to meet new needs.

Enhanced Flexibility: Depending on our needs, we may make the variables of the class read-only or write-only.

Inheritance

The process of acquiring the property and behaviour of one object into another object is, the same as a child inheriting the characters of their parents. This phenomenon of reusing the property and behaviour of a parent class into a child class is known as Inheritance.

Simply by using extends keyword, we can inherit the data members and methods of any class into another one.

inheritance.-in-java

From the above Code snippet, we can observe that method show() is not visible in the Child class but still, it is used in the class (without writing the same method again in child classes, it can be used many times). So due to this powerful concept length of code is reduced as we can write a method one time and can use it anywhere we required.

Apart from these some other concepts like Abstraction, and the use of Interface enhanced the function of OOPs in Java

Types of Inheritance –>

1.Single Inheritance –

It is very easy to understand. When a class extends another class only in that case we say that it is single inheritance. The diagram below shows that class B extends only one class, class A (Parent). Here class A is a parent class of class B and B would be a child class of A.

single-inheritance

Code Syntax:- To represent the above flow diagram

code-syntax

2.Multi-Level Inheritance –

Multilevel inheritance refers to a procedure where one can inherit from a derived class, thereby making this derived class the base class for the new class. As you can see in the below flow diagram, class C is a subclass or child class of class B and class B is a child class of class A.

multi-level-inheritance

Code Syntax:- To represent the above flow diagram

multi-level-code-syntax

3.Hierarchical Inheritance-

When more than one class inherits the same class then this is called hierarchical inheritance. For example, in java, there is an Object class which is inherited by all classes by default.
In the hierarchical type of inheritance, one class is extended by many subclasses. It is a one-to-many relationship.

hierarchical-inheritance

Code Syntax:- To represent the above flow diagram

hierarchical-nheritance-syntax

Conclusion

Overall, Procedural and object-oriented programming approaches are two major programming approaches. The main difference between procedural and object-oriented programming is that procedural programming helps to develop a program using a set of modules or functions while object-oriented programming helps to construct a program using a set of objects and their interactions.

Some types of applications favour Procedural Programming (like compilers), while others are more flexible for using OOP principles (like classic desktop applications, and web applications). Choosing the right programming approach depends on several factors, like the target programming language, framework, and type of application you are building. Use what you feel is most comfortable, but do not forget to keep on learning to add new concepts in mind.

bluethinkinc_blog

bluethinkinc_blog

2022-12-06

0

Leave a Reply

Your email address will not be published. Required fields are marked *

Find More Blogs

How to Add a Custom Tab to the Customer Order Detail Page in Magento 2

Spread the love Adding a custom tab to the Order Detail

Exception Handling in Java

Spread the love Introduction – Exception:An unwanted

How to add custom less file in Magento2

Spread the love Source files *.css included into layout

Spring Boot Microservices

Spread the love Microservices architecture has become

Implementation of WireMock for better testing

Spread the love WireMock is a flexible and powerful tool

Magento 2: How to selected default option in configurable options?

Spread the love Configurable products in Magento 2 allow

How To Implement Simple JWT Authentication in Django Rest Framework

Spread the love We’ll learn how to use simple JWT

Optimizing Search: Integrating Elasticsearch in Django Applications

Spread the love What is Elasticsearch? Elasticsearch is a

How to create Customer Segments

Spread the love In Magento 2, “customer

Proxy Design Pattern and Requisition List in Magento 2

Spread the love Requisition List in Magento 2 Requisition

bluethinkinc Blogs