DEV Community

Sundar Joseph
Sundar Joseph

Posted on

Java OOP(Object Oriented Programming) Concepts :

Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.

The core idea of OOPs is to bind data and the functions that operate on it, preventing unauthorized access from other parts of the code. Java strictly follows the DRY (Don't Repeat Yourself) Principle, ensuring that common logic is written once (e.g., in parent classes or utility methods) and reused throughout the application. This makes the code:

What is OOPs and Why Do We Use it?
OOPS stands for Object-Oriented Programming System. It is a programming approach that organizes code into objects and classes and makes it more structured and easy to manage. A class is a blueprint that defines properties and behaviors, while an object is an instance of a class representing real-world entities.

example :

// Use of Object and Classes in Java
import java.io.*;

class Numbers {
// Properties
private int a;
private int b;

// Setter methods
public void setA(int a) { this.a = a; }
public void setB(int b) { this.b = b; }

// Methods
public void sum() { System.out.println(a + b); }
public void sub() { System.out.println(a - b); }

public static void main(String[] args)
{
    Numbers obj = new Numbers();

    // Using setters instead of direct access
    obj.setA(1);
    obj.setB(2);

    obj.sum();
    obj.sub();
}
Enter fullscreen mode Exit fullscreen mode

}

Output :

3
-1

Java Class :

A Class is a user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. Using classes, you can create multiple objects with the same behavior instead of writing their code multiple times. This includes classes for objects occurring more than once in your code. In general, class declarations can include these components in order:

Java Object :

An Object is a basic unit of Object-Oriented Programming that represents real-life entities. A typical Java program creates many objects, which as you know, interact by invoking methods. The objects are what perform your code, they are the part of your code visible to the viewer/user. An object mainly consists of:

Top comments (0)