Java OOP
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Java OOP

Jeevadharshan

 What Is OOP in Java? 

Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around objects rather than functions and procedures. In Java, an object combines data (fields) and behavior (methods) into a single reusable unit. 

In contrast, procedural programming focuses on writing sequences of instructions (procedures) that operate on data separately. OOP models real-world entities—such as cars, users, or products making programs more intuitive, modular, and scalable. 

Advantages of OOP in Java 

Object-oriented design provides several practical benefits for modern software development:
  • Modularity: Programs are structured into independent objects and classes 
  • Code Reusability: Classes can be reused across projects and modules 
  • Maintainability: Changes in one class have minimal impact elsewhere 
  • Scalability: Easier to extend applications with new features 
  • Encapsulation: Data is protected and accessed through controlled methods 
  • DRY Principle: Avoids duplication by centralizing shared logic
DRY (Don’t Repeat Yourself): Place common functionality in one location (e.g., a class or method) and reuse it instead of duplicating code. 

Classes and Objects in Java

The foundation of OOP in Java consists of classes and objects.
  • Class: A blueprint or template that defines properties and behavior 
  • Object: A real instance created from a class 

Real-World Analogy 

Table

Class (Blueprint) 
Fruit   
Objects (Instances) 
Apple, Banana, Mango 
Car 
Toyota, Audi, Tesla 
 Student  Asha, Ravi, Meena

When an object is created from a class, it automatically receives all the fields and methods defined in that class.  

Java Class and Object Example1 

The following original program demonstrates how a class defines properties and methods, and how multiple objects are created from it. 
 
class Car { 
    String brand; 
    int speed; 
 
    // Constructor 
    Car(String brand, int speed) { 
        this.brand = brand; 
        this.speed = speed; 
    } 
 
    void showDetails() { 
        System.out.println("Brand: " + brand + ", Speed: " + speed + " km/h"); 
    } 
 
public class CarDemo { 
    public static void main(String[] args) { 
        Car car1 = new Car("Toyota", 180); 
        Car car2 = new Car("Tesla", 220); 
 
        car1.showDetails(); 
        car2.showDetails(); 
    } 
}  

Output 

Brand: Toyota, Speed: 180 km/h 
Brand: Tesla, Speed: 220 km/h  

Java Class and Object Example2 

// The Class — a blueprint for a BankAccount 
class BankAccount { 
    String owner; 
    double balance; 
 
    void deposit(double amount) { 
        balance += amount; 
        System.out.println(owner + " deposited $" + amount + " | New balance: $" + balance); 
    } 
 
    void withdraw(double amount) { 
        if (amount > balance) { 
            System.out.println("Insufficient funds for " + owner); 
        } else { 
            balance -= amount; 
            System.out.println(owner + " withdrew $" + amount + " | New balance: $" + balance); 
        } 
    } 
 
// The Main Class — where objects are created and used 
public class Main { 
    public static void main(String[] args) { 
        // Two independent objects from the same class 
        BankAccount alice = new BankAccount(); 
        alice.owner = "Alice"; 
        alice.balance = 1000.00; 
 
        BankAccount bob = new BankAccount(); 
        bob.owner = "Bob"; 
        bob.balance = 500.00; 
 
        alice.deposit(250.00); 
        bob.withdraw(100.00); 
        alice.withdraw(2000.00); 
    } 
}  

 Output:

Alice deposited $250.0 | New balance: $1250.0 
Bob withdrew $100.0 | New balance: $400.0 
Insufficient funds for Alice

In this example, BankAccount is the class. alice and bob are two distinct objects — each with their own owner and balance data, but sharing the same deposit and withdraw behavior defined in the class. Neither object affects the other's state.


Tags
Our website uses cookies to enhance your experience. Learn More
Accept !