Object-Oriented Programming in Java

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects, which can contain data (fields) and methods (functions). Java is a fully object-oriented language (except for primitive types), and understanding its OOP principles is essential for effective Java development.


๐Ÿ”‘ Core Principles of OOP in Java

Encapsulation


Inheritance


Polymorphism


Abstraction


1. ๐Ÿ›ก️ Encapsulation

Encapsulation means hiding internal details of a class and exposing only what is necessary through public methods (getters/setters).


java

Copy

Edit

public class Person {

    private String name; // private = encapsulated

    private int age;


    public String getName() {

        return name;

    }


    public void setName(String newName) {

        name = newName;

    }

}

2. ๐Ÿงฌ Inheritance

Inheritance allows one class (subclass) to inherit fields and methods from another class (superclass), promoting code reuse.


java

Copy

Edit

class Animal {

    void speak() {

        System.out.println("Animal speaks");

    }

}


class Dog extends Animal {

    void bark() {

        System.out.println("Dog barks");

    }

}

3. ๐ŸŽญ Polymorphism

Polymorphism means "many forms". In Java, this allows you to call the same method on different objects and get different results.


Compile-time Polymorphism (Method Overloading):

java

Copy

Edit

class MathUtil {

    int add(int a, int b) {

        return a + b;

    }

    double add(double a, double b) {

        return a + b;

    }

}

Runtime Polymorphism (Method Overriding):

java

Copy

Edit

class Animal {

    void makeSound() {

        System.out.println("Animal sound");

    }

}


class Cat extends Animal {

    @Override

    void makeSound() {

        System.out.println("Meow");

    }

}

4. ๐Ÿงฉ Abstraction

Abstraction means hiding complex implementation details and showing only essential features.


Abstract Class Example:

java

Copy

Edit

abstract class Vehicle {

    abstract void drive(); // abstract method

}


class Car extends Vehicle {

    void drive() {

        System.out.println("Driving a car");

    }

}

Interface Example:

java

Copy

Edit

interface Flyable {

    void fly();

}


class Bird implements Flyable {

    public void fly() {

        System.out.println("Bird is flying");

    }

}

๐ŸŽฏ Why Use OOP in Java?

Modularity: Code is organized into classes.


Reusability: Inheritance allows you to reuse existing code.


Scalability: Easy to manage and scale large applications.


Security: Encapsulation protects data and logic.


๐Ÿ›  Real-World Example

java

Copy

Edit

class BankAccount {

    private double balance;


    public void deposit(double amount) {

        if (amount > 0)

            balance += amount;

    }


    public void withdraw(double amount) {

        if (amount <= balance)

            balance -= amount;

    }


    public double getBalance() {

        return balance;

    }

}

Let me know if you’d like help building an object-oriented Java project or more examples on any concept!

Learn Full Stack JAVA Course in Hyderabad

Read More

Core Java Concepts

Using Bootstrap in Frontend Development

Visit Our IHUB Talent Training Institute in Hyderabad

Get Directions

Comments

Popular posts from this blog

Handling Frames and Iframes Using Playwright

Cybersecurity Internship Opportunities in Hyderabad for Freshers

Tosca for API Testing: A Step-by-Step Tutorial