Understanding OOPS Concepts

By technoayan
0views
Understanding OOPS Concepts

A detailed overview of the core concepts of Object-Oriented Programming (OOPS) with examples to help developers understand the

🎯 Object-Oriented Programming (OOP) Concepts in Java

🚀 Overview

Object-Oriented Programming (OOP) in Java allows developers to create reusable, modular, and scalable code by structuring it around objects and classes.


🌟 Core Concepts

1. 🎭 Class and Object

  • Class: A blueprint for creating objects.
  • Object: An instance of a class.
JAVA
// Class definition
public class Car {
    String brand;
    String model;

    // Constructor
    public Car(String brand, String model) {
        this.brand = brand;
        this.model = model;
    }

    // Method
    public void displayDetails() {
        System.out.println("Brand: " + brand + ", Model: " + model);
    }
}

// Creating an object
Car myCar = new Car("Tesla", "Model S");
myCar.displayDetails();

2. 🔒 Encapsulation

  • Restricts access to certain parts of an object.
  • Uses private fields with getter and setter methods.
JAVA
public class BankAccount {
    private double balance;

    // Constructor
    public BankAccount(double balance) {
        this.balance = balance;
    }

    // Getter method
    public double getBalance() {
        return balance;
    }

    // Setter method
    public void deposit(double amount) {
        balance += amount;
    }
}

3. 🧬 Inheritance

  • Enables one class (child) to inherit fields and methods from another class (parent).
JAVA
// Parent class
public class Vehicle {
    public void startEngine() {
        System.out.println("Engine started!");
    }
}

// Child class
public class Car extends Vehicle {
    public void playMusic() {
        System.out.println("Playing music!");
    }
}

// Usage
Car myCar = new Car();
myCar.startEngine();
myCar.playMusic();

4. ⚡ Polymorphism

  • Allows methods to take different forms (overloading and overriding).
JAVA
// Overloading
public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }

    public double add(double a, double b) {
        return a + b;
    }
}

// Overriding
class Animal {
    public void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    @Override
    public void sound() {
        System.out.println("Dog barks");
    }
}

5. 🏗️ Abstraction

  • Hides implementation details and exposes only essential features.
JAVA
abstract class Animal {
    abstract void sound(); // Abstract method

    public void sleep() { // Concrete method
        System.out.println("Sleeping...");
    }
}

class Cat extends Animal {
    @Override
    void sound() {
        System.out.println("Cat meows");
    }
}

// Usage
Animal cat = new Cat();
cat.sound();
cat.sleep();

📌 Key Benefits of OOP

  • 📦 Reusability: Reuse code through inheritance.
  • 🔍 Data Security: Encapsulation hides sensitive data.
  • 🚀 Scalability: Easily extend features using polymorphism.
  • 🛠️ Maintainability: Abstraction simplifies code updates.

Thanks for reading!

technoayan

Author & Tech Enthusiast

"Keep learning, keep growing, and keep sharing knowledge with the world."

Show Some Love

Let the author know you enjoyed this content

0
people like this

Sign in to show your appreciation

Share This Post

Help others discover this content

Rate This Post

Share your thoughts and help others discover great content

Sign in to rate this post and share your feedback

Community Rating

No ratings yet. Be the first to rate this post!

Comments (0)

Leave a Comment

No comments yet. Be the first to share your thoughts!