OOPs concept in Java

Object-Oriented Programming (OOP) is a crucial concept in Java that simplifies the process of software development by organizing code around objects. In Java, OOP allows programmers to create reusable and modular code, making it easier to build complex applications. The key principles of OOPs concept in Java include encapsulation, inheritance, and polymorphism. You will have a solid understanding of object-oriented programming in Java and how to use it to create robust and scalable programs at the end of this article.

Introduction to OOPs concept in Java

OOPs concept in Java is a programming paradigm that revolves around objects. An object is a real-world entity that has a set of attributes and behaviors. In Java, even basic data types like integers and booleans are considered objects.

In OOPS, a program is designed by creating objects that interact with each other to perform tasks. Objects have attributes that define their properties and methods that define their behavior.

In Java, we create classes, which are templates that define the attributes and behaviors of objects. A class can be compared to an object creation blueprint. Once a class is defined, objects can be created based on that class.

Principles of Object-Oriented Programming

OOPs concept in Java

There are four main principles of object-oriented programming:

a. Abstraction

Abstraction is the process of hiding complex details and providing a simplified interface for the user. It helps developers to create simpler and more understandable models of a complex system, by just exposing only the required features and not the implementation part. In Java, abstraction can be achieved using interfaces and abstract classes.

b. Encapsulation

Encapsulation is the process of hiding the internal details of an object and providing a public interface for accessing and modifying the object. It helps in maintaining the integrity of the object and ensures that its internal state cannot be modified by external code. In Java, encapsulation can be achieved using access modifiers such as public, private, and protected.

Got confused between Abstraction and Encapsulation?
Abstraction is about simplifying the complexity of a system by focusing on its essential features, while encapsulation is about protecting the integrity of an object by hiding its internal details.

c. Inheritance

The process of creating a new class from an existing one is called Inheritance. The new class inherits the attributes and behaviors of the existing class and can add additional attributes and behaviors. This helps in reducing code duplication, promoting code reuse, and making the code more modular and maintainable. In Java, the extend keyword is useful for achieving inheritance.

d. Polymorphism

The ability of an object to take on multiple forms is known as polymorphism. The Java language supports polymorphism through method overloading and method overriding.

Classes and Objects in Java

A class is a blueprint for creating objects. It defines the attributes and behaviors of an object. An object is an instance of a class. It has a unique set of characteristics and behaviors.

To create a class in Java, use the following syntax:
Let’s see an example to understand
1. Class Creation – You can use the class keyword to create a class.
2. Object – It can be created using a new keyword.
3. Method

public class CodeAvinya {
	
	static String name;
	static float salary;
        
	static void set(String n, float p) {
		name = n;
		salary = p;
	}

	static void get() {
		System.out.println("Employee name is: " + name );
		System.out.println("Employee CTC is: " + salary);
	}

	public static void main(String args[]) {
		// object
                CodeAvinya obj = new CodeAvinya();
                obj.set("Rajesh", 15000.0f);
		obj.get();
	}
}

To create an object based on a class, use the following syntax:

MyClass obj = new MyClass();

Inheritance

The technique of building a new class from an existing one is called inheritance. The new class inherits the attributes and behaviors of the existing class and can add additional attributes and behaviors.

To create a subclass in Java, use the following syntax:

public class MySubclass extends MyClass{
    // subclass body
}

Polymorphism

Polymorphism refers to an object’s capacity to assume various forms.  The overloading and overriding of methods are two ways to implement polymorphism in Java. This allows developers to reuse existing code and also create new classes which are tailored to the specific application or system.

Creating multiple methods with the same name but different parameters is called method overloading. Method overriding is the process of creating a new implementation of a method that already exists in a superclass.

Encapsulation

Encapsulation, like a class in Java, describes grouping together data and functions that operate on that data into a single unit. This idea is frequently used to conceal the internal representation or state of an entity. We refer to this as hiding of the information. In Java, this is achieved by using access modifiers such as public, private, and protected. Encapsulation helps to ensure that the data inside an object is only accessible through a well-defined interface, which allows for improving security and maintainability.

Abstraction

Abstraction is the process of hiding complex implementation details behind a more straightforward interface.Java interfaces and abstract classes are used for achieving abstraction. By using abstraction, developers can create code that is easier to understand and maintain, as complex implementation details are hidden from the outside world.

Conclusion

Object-oriented programming is an important programming paradigm that allows developers to create more flexible, maintainable, and reusable code. Java is a language that fully supports object-oriented programming and provides developers with a rich set of features and tools to create and manipulate objects. By understanding the principles of object-oriented programming, and following best practices for implementing it in Java, developers can create easier code to understand, maintain, and extend. Whether you’re a beginner or an experienced developer, understanding object-oriented programming in Java is a critical skill that will help you become a better programmer.