×

Object Oriented Programming (OOPs)

Since the dawn of earth, we have kept on evolving. The need for evaluation comes with the aim of improving the existing systems when something inappropriate is found for the environment. Same is true in the case of programming languages. We need development in programming languages as well. Before discussing what led to the emergence of OOP, let us take a brief look at what led to the need for such a system.

Starting with the development of the first programming language in the 19th century, we kept on evolving with the needs and drawbacks of the preceding languages.

Then, there emerged a need for a high-level language which is structured, efficient and could possibly replace the assembly code. This led to the development of C language which was a Procedure Oriented Programming Language (POP).

Procedure Oriented Programming Language (POP)

In POP, the instructions are written under the main function, and each line specifies the instruction to be performed by the computer. When code size increases, the program is divided into several user-defined functions. In POP, the data is given the second priority. The codes in POP were written for a specific purpose and could not be used elsewhere.

C language shook the computer world; it appeared as if a perfect language had been found which is well equipped and sufficient for the development of system code.

With every perfect thing, there come some disadvantages, the same was true for C.  Codes in small and medium size were easily handled, but the long-sized codes became difficult to manage and manipulate.

Disadvantage of Procedure Oriented Programming Language (POP)

There were the following disadvantages which were faced with this approach of programming:

  • Every function could access the global variables; hence necessary changes were required to be made in function definition so that manipulation of data values can be done only when required.
  • This approach of programming was not suitable for real-life problems.
  • Adding new features to existing code was a daunting task.

In order to track the above-mentioned issues and overcome from them there was a need to develop a system of programming in which:

    • The emphasis is mainly on data.
    • Once written could be reused for other purposes.
    • New methods and variables could be added to the existing code whenever required without the need to write the whole code.

And hence the object-oriented programming system was developed.

Object-oriented Programming

Object Oriented Programming is an approach in which the programs are standardized. A special memory area is created for the methods and data. This approach of programming revolves around ‘objects’, which are nothing but real-world entities. This methodology of programming helps to organize complex programs using concepts of inheritance, polymorphism, and data abstraction. In simple terms, the approach in OOP is to firstly create a class that has a large number of methods and variables and then using the class the state of objects is determined.  Hence class is nothing but a blueprint for determining the state of objects and objects are the real world entities.

Let us take a look at the concepts of OOP

1. Class:  Class is a blueprint to give shape to the real world entities called the objects. A class contains a number of variables and methods. Class doesn’t have any physical existence, and they do not occupy any space on their own.

E.g. Fruit, Building, Shape, Vehicle, etc.

2. Objects: They are the soul component of the OOP. They are real-world entities with a state and behavior. Any program is analyzed with the help of objects and their behavior. Objects are associated with classes, and they obtain their structure from the class. There can be more than one objects associated with the same class. Each of these objects can communicate with each other. Objects occupy their space in memory.

E.g. mango, banana, apple, car, bus, person, etc.

3. Inheritance: A very important concept of OOP is an inheritance. It is property by which a class can succeed the other class. It supports hierarchical programming. We have a class which has some methods and variables defined, and if there is a need to make a class which has all the properties of the previous class with some additional features of its own then it can be achieved by inheritance. The class which is previously defined is the parent class or the superclass and the class which inherits the features of the previously defined class is called the child class. For e.g.

In the figure shown above, all the features of polygon are present in quadrilateral and hexagon. Quadrilateral later extends to rectangle and kite.

4. Polymorphism: Poly means ‘many’ and ‘morph’ means form. So polymorphism is the ability to take many forms. For example, remember the childhood days when while watching ghost programs the ghost was able to take the shape of any human being as well as animal or any other possible object as needed by him. This is what polymorphism means. An entity can take several different forms as and when required.

There are two types of polymorphism:

    • Compile time polymorphism
    • Run time polymorphism

5. Encapsulation: Encapsulation is the act of wrapping up or binding of the data and code together into a single entity. It is very important as it helps to achieve data hiding and data security in our code. It can be achieved in many ways. One of them is to use the various access specifiers provided. Other way is to use nesting of class, the class which contains the private code is covered by another class which can only access the private class.

6. Data Abstraction: It is an act of showing only the necessary information, hiding the background details. We use abstract class and interfaces in java to achieve data abstraction. It is also an important concept as the users only get to see the relevant information not the internal functioning.

E.g. when we only mail account, internal processing is not shown.

Benefits of Object-oriented Programming

  1. Code once written can be reused further.
  2. With the help of inheritance whenever there is a need for a class extension, it can be achieved.
  3. Since the code is divided into methods and variables, adding features to the existing code is easy.
  4. Error detection and correction is easy.
  5. There can be various instances of the same code at the same time.
  6. Up gradation is easier.
  7. Code which needs to be kept secure from the outside world can be significantly achieved.
  8. Code redundancy does not occur.
  9. Software complexity can be easily managed.
  10. Work partitioning is easy in this approach of programming.

Related Topics

ArrayList Program in Java

ArrayList Program in Java: In Java, ArrayList is a class that belongs to java.util package. It is the dynamic list that grows or shrinks at run-time as per the requirements....

4 minutes read.

XOR Binary Operator in Java

One of the various Bitwise operators in Java is ava XOR. If two boolean operands are given, the XOR (also known as exclusive OR) returns true. When both of the...

4 minutes read.

Tree Implementation in Java

Introduction to Tree A non-linear, hierarchical data structure called a "tree" is made up of a number of nodes, each of which contains a values and a sequence of pointers to...

14 minutes read.

Why String in Immutable in Java?

Why String in Immutable in Java Immutable means unchangeable or unmodifiable.  Strings in Java are immutable, it means once a string is created, it cannot be modified or changed. Any change...

2 minutes read.

Java Math copySign() Method

The copySign() method of Math class returns the first floating-point argument with the sign of the second argument. Syntax: public static float copySign(float magnitude, float sign)public static double copySign(double magnitude, double sign) Parameters: The...

1 minute read.

How to Solve the Deprecated Error in Java?

Deprecated Java methods should not be used because they are deprecated (often, there are better, more modern alternatives). API update. Until now, Java has kept everything backward compatible and never...

3 minutes read.

How to find the length of an Array in Java

Arrays: An array is a sort of container object that stores constant quantities of values of a single type in one memory area. A finite number of items must all be...

3 minutes read.

Prime Points in Java

The points that divide an integer into two halves containing a prime number are known as prime points. Printing every prime point of a specific number is the task. Let's...

6 minutes read.

Lazy Propagation in Segment Tree in Java

The topic of segment trees in Java is continued by the topic of sluggish propagation in segment trees. It is suggested that readers first read through the section tree topic....

4 minutes read.

Reserved Keywords in Java

In Java, a reserved term is used as a code key called a keyword. Because they are predefined, these terms cannot be used for anything else. They cannot serve as...

3 minutes read.

Converting Long to Date in Java

What Long and Date are in Java and how are they implemented in the Java programming language are the topics of this article. Additionally, we'll go into great detail on...

4 minutes read.

Example of Static import in Java

Static keyword Java's static keyword is mainly used to control memory. Variables, methods, blocks, and nested classes are compatible with the static keyword. Instead of an instance, the static keyword is...

3 minutes read.

What’s new in Java 12

On March 19th, 2019, the Java 12th edition was released. After releasing this edition, they have decided to release every new edition every six months. This version is the advanced...

5 minutes read.

Java Integer toString() method

The toString() method of Java Integer class returns a String object which represents this Integer’s value. The second syntax returns a String object which represents the specified integer. The third syntax returns...

2 minutes read.

Java Calculate Average of List

The list is a linear data structure used in Java to store ordered data collections. Additionally, it accepts duplicate values while maintaining insertion order. It is sometimes necessary to find...

3 minutes read.

Web Crawler in Java

In this article, you will be acknowledged with what a web crawler in java is and what are its functions. You will also be able to understand where to implement...

4 minutes read.

How to Convert char to int in Java

How to Convert char to int in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

3 minutes read.

Rotate matrix by 90 degrees in Java | Rotate matrix in Java clockwise and anti-clockwise

In this article, you will be acknowledged about what is a matrix along with an example. Most importantly you will be equipped knowledge on how to rotate the matrix by...

4 minutes read.

Java Program to Add Digits Until the Number Becomes a Single Digit Number

We will develop Java programme that increase a number's digit count in order to reduce it to one. The issue is also known as the digit root problem. Example Consider the case where...

3 minutes read.

Difference Between Access Specifiers and Modifiers in Java

Java employs access modifiers to restrict a class's data members, member functions, and constructor. Access modifiers are essential when creating Java program and applications. Access modifiers in Java include: defaultpublicprotectedprivate Default Access Modifiers Without...

4 minutes read.