×

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

Java Boolean hashCode() Method

The hashCode() method of Boolean class returns the hash code for the specified Boolean object or the given Boolean value. Syntax public int hashCode()public int hashCode(boolean value) Parameters The parameter ‘value’ represents the value...

2 minutes read.

Java Math cos() Method

The cos() method of Math class returns the trigonometric cosine of the specified angle. Syntax: public static double cos(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The cos() method returns...

1 minute read.

Java Math floorMod() Method

The floorMod() method of Math class returns the floor modulus of the specified arguments. It firstly divides the dividend and divisor and then returns an integer that is equal to...

1 minute read.

Stack Program in Java

Stack Program in Java The Stack class is part of the collection framework that inherits the Vector class. Thus, the Stack class can also be called a subclass of the Vector...

5 minutes read.

Java Location

PATH is an environmental variable that the system software now uses to find executable files (.exe) or Java binaries ( java or javac command). Once chosen, a route cannot be...

3 minutes read.

Java LinkedHashSet

LinkedHashSet in Java with Example Java LinkedHashSet extends HashSet and Implements the Set interface. It doesn’t contain only duplicate values like HashSet. It also permits the null elements. It maintains the order...

5 minutes read.

String vs StringBuilder

String vs StringBuilder In this section, we will discuss the comparison between Java String and StringBuilder class. String In Java, a string is treated as an object that represents a sequence of characters....

4 minutes read.

Balanced Parentheses in Java

One of the frequent programming issues, commonly referred to as the Balanced brackets issue, is the problem with the balanced parenthesis. Interviewers frequently provide this task, in which we must...

5 minutes read.

Java Math toDegrees() Method

The toDegrees() method of Java Math class converts a radian angle to an approximately equivalent angle measured in degrees. Syntax: public static double toDegrees(double angrad) Parameters: The parameter ‘angrad ‘represents an angle measured in...

2 minutes read.

Java program to print matrix in Z form

In this article, you will be acknowledged about what is a matrix along with an example. Also, most importantly, you will learn how to print matrix in Z form. What is...

5 minutes read.

Advantages and Disadvantages of Strings in Java

What is a String? Java is an object-oriented, platform-independent, high-level, general-purpose, and interpreted programming language.Sun Microsystems is known as the founder of java in 1991; the Java programming language was developed...

4 minutes read.

Java Imageio

The javax.imageio package contains the final class known as Java ImageIO. For easy image reading, writing, and simple encoding and decoding, the class offers a convenient way. The class offers...

4 minutes read.

Java Case Keyword

Case keyword: In this article we are going to learn the concept of a java case keyword.Generally, java case keyword is used with the switch statements.Case keyword is implemented in conditional...

3 minutes read.

How to Split String by Comma in Java

strsplit() technique permits you to break a string given the explicit Java string delimiter. The Java string split property is frequently a space or a comma(,) that you want to...

7 minutes read.

Practical Number in Java

In this tutorial, we will understand what is meant by practical numbers. We will understand it throughthe aid of examples and implementation in a java programming language. The practical numbers...

5 minutes read.

How to add 24 Hours to Date in Java?

In this tutorial, we will learn how to add 24 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

How to Reduce Time Complexity in Java

What is time complexity?  The time complexity in java is given as the amount of time a program requires to run or execute it Calculating the time complexity of the program The time...

4 minutes read.

Java Exception Propagation

Java Exception Propagation When an exception is being thrown from the peak of the stack and not getting caught, it runs down the stack to the previous method, which is sitting...

3 minutes read.

Untouchable Number in Java

If a number N cannot be divided properly by any positive number, it is said to be an untouchable number. Additionally known as nonaliquot numbers. The sequence is A005114 from...

3 minutes read.

Convert IP to Binary in Java

Fundamental conversion, such as going from binary to decimal or vice versa, is a crucial activity in computers. Understanding IP addressing and subnetting is crucial for networking. The primary networking...

4 minutes read.