Object Oriented Concepts in Java

Object Oriented Concepts in Java

Object-oriented programming is all about using the real-world concept of an object in the programming world. Here real-world entities like Inheritance, Polymorphism, Binding, etc. come into the picture. We will be covering the following OOP Concepts in this article:
  • Polymorphism

  • Inheritance

  • Encapsulation

  • Abstraction

Before going into the details, let’s find out some of the benefits of using the OOP concept in programming.

 

 

Benefits of Object Oriented Programming in Java:

  • Reusability: OOP principles like Inheritance, Composition, and Polymorphism help in reusing existing code. In OOP, you will never code the same block again in your program, rather reuse the existing block.

  • Extensibility: Code written using OOP principles like Inheritance makes the code extensible.

  • Security: OOP principles like Encapsulation helps to keep the data and the code operating on that data secure.

  • Simplicity: Java classes represent real world objects. This makes the code very easy to understand. For example, in real life a bus is an object which has attributes like color, weight, height, etc., and methods such as drive and break.

  • Maintainability: Code written using OOP concepts is easier to maintain.

 

Now, let’s look into the main OOP concepts:

 

 

 

Polymorphism:

Polymorphism is the ability to use the same interface to execute different interface codes. Java achieves Polymorphism via method overloading and overriding.

In Java, the language can differentiate between entities having the same name efficiently.

Consider the following example:  

 

 

 

class multiplicationFunction {  

// method with 2 parameters  

static int multiply(int a, int b)  

{    

return a * b;  

}    

// method with the same name but 3 parameters    

static int multiply(int a, int b, int c)  

{    

return a * b * c;  

}  

}  

class Main {    

public static void main(String[] args)  

{    

System.out.println( multiplicationFunction.multiply(2, 6) );    

System.out.println( multiplicationFunction.multiply(6, 4, 2) );  

}

}

 

 

Though all the methods have the same name, the program will compile successfully and will provide output.

 

 

There are two kinds of methods in Java:
  1. Compile-time polymorphism, also known as static binding or method overloading

  2. Run-time polymorphism, also known as dynamic binding or method overriding

 

Inheritance:

Like we see inheritance in the real-world, Java classes can also share or inherit properties or methods of other classes. This way, we can reuse the code once written in many other places in a program. The class that inherits properties from another class is called Derived Class or Child Class. The class that shares its properties with another class is called the Base Class or Parent Class.

 

In Java, we can use this feature by inserting ‘extends’ keyword:

 

 

 

public class Vehicle {

 

  String vehicleType;

 

  String vehicleModel;

 

  void mileage() { }

 

}

 

public class Car extends Vehicle {

 

}

 

 

The inherited Car Class can use all the variables and methods of Vehicle Class.

 

 

Encapsulation:

Encapsulation refers to keeping objects with their methods in one single place. It also protects the integrity of the information– prevents it from being needlessly altered by restricting access to the data, preferably by hiding it from outside elements. Encapsulation is usually confused with data abstraction, but they are different concepts entirely. Data hiding, or data abstraction, has more to do with access specifiers. A programmer must first encapsulate the information; only then he can take steps to cover it.

 

Procedural programs, for example, are not encapsulated. The procedures are grouped separately from the data. In OOP, the given data is usually grouped along with side methods that operate upon the information.

 

In Java, encapsulation is built-in and whenever you create a class, this principle is followed naturally.

 

 

Abstraction:

This feature in OOP aims to hide the complexity from the users and provide them with relevant information only. There are abstract classes or interfaces available in Java through which we can provide only the required information to the users, hiding all unwanted code.
 
There are two types of abstractions commonly used in Java:
  1. Data Abstraction

  2. Control Abstraction

 

Consider the following code block:

 

 

abstract class Animal{

 

  public abstract void animalSound();

 

  public void sleep() {

 

    System.out.println(“Zzz”);

 

  }

 

}

 

 

Here you won’t be able to create an object for Animal class. Animal animalObj = new Animal(); will generate error.

 

If we want to access an abstract class, it must be inherited from another class. So, in the above example:

 

 

 

class Lion extends Animal {

 

  public void animalSound() {

 

  }

 

}

 

 

We create a class Lion that extends Animal class, now we can create an object for the Lion class: Lions lionObject = new Lion();

 

 

End Note:

In this blog, we looked at Object-Oriented Programming (OOP) concepts. We also saw the benefits of this type of programming in Java. Some of the main OOP concepts like polymorphism, encapsulation, and inheritance, among other concepts, were briefly touched upon.

 

So, it’s important to understand these concepts in-depth to make use of the power of Object-Oriented Programming. Happy Learning!!

 

 

Get one step closer to your dream job!

 

Prepare for your Java programming interview with Core Java Questions You’ll Most Likely Be Asked. This book has a whopping 290 technical interview questions on Java Collections: List, queues, stacks, and Declarations and access control in Java, Exceptional Handling, Wrapper class, Java Operators, Java 8 and Java 9, etc. and 77 behavioral interview questions crafted by industry experts and interviewers from various interview panels. 

We’ve updated our blogs to reflect the latest changes in Java technologies.
This blog was previously uploaded on March 28th, 2020, and has been updated on January 19th, 2022.