simple and practical Java examples for learning Object-Oriented Programming (OOP) concepts

 

Here are some simple and practical Java examples for learning Object-Oriented Programming (OOP) concepts:


1. Class and Object

Concept: A class is a blueprint, and an object is an instance of a class.

class Car {

    String brand;

    int speed;

 

    void displayInfo() {

        System.out.println("Brand: " + brand + ", Speed: " + speed);

    }

}

 

public class Main {

    public static void main(String[] args) {

        Car car1 = new Car();

        car1.brand = "Toyota";

        car1.speed = 120;

        car1.displayInfo();

    }

}


2. Encapsulation

Concept: Hiding the internal state of the object and providing access through methods.

class BankAccount {

    private double balance;

 

    public void deposit(double amount) {

        if (amount > 0) {

            balance += amount;

        }

    }

 

    public void withdraw(double amount) {

        if (amount > 0 && amount <= balance) {

            balance -= amount;

        }

    }

 

    public double getBalance() {

        return balance;

    }

}

 

public class Main {

    public static void main(String[] args) {

        BankAccount account = new BankAccount();

        account.deposit(1000);

        account.withdraw(300);

        System.out.println("Balance: " + account.getBalance());

    }

}


3. Inheritance

Concept: Reusing code by inheriting properties and methods from a parent class.

class Animal {

    void eat() {

        System.out.println("This animal eats food.");

    }

}

 

class Dog extends Animal {

    void bark() {

        System.out.println("The dog barks.");

    }

}

 

public class Main {

    public static void main(String[] args) {

        Dog dog = new Dog();

        dog.eat();

        dog.bark();

    }

}


4. Polymorphism

Concept: Methods behave differently based on the object or method parameters.

a. Method Overloading

class Calculator {

    int add(int a, int b) {

        return a + b;

    }

 

    int add(int a, int b, int c) {

        return a + b + c;

    }

}

 

public class Main {

    public static void main(String[] args) {

        Calculator calc = new Calculator();

        System.out.println(calc.add(5, 10));

        System.out.println(calc.add(5, 10, 15));

    }

}

b. Method Overriding

class Shape {

    void draw() {

        System.out.println("Drawing a shape.");

    }

}

 

class Circle extends Shape {

    @Override

    void draw() {

        System.out.println("Drawing a circle.");

    }

}

 

public class Main {

    public static void main(String[] args) {

        Shape shape = new Shape();

        shape.draw();

 

        Shape circle = new Circle();

        circle.draw();

    }

}


5. Abstraction

Concept: Hiding implementation details while exposing only essential functionalities.

abstract class Vehicle {

    abstract void start();

 

    void stop() {

        System.out.println("Vehicle has stopped.");

    }

}

 

class Car extends Vehicle {

    @Override

    void start() {

        System.out.println("Car is starting.");

    }

}

 

public class Main {

    public static void main(String[] args) {

        Vehicle car = new Car();

        car.start();

        car.stop();

    }

}


6. Interface

Concept: Defining a contract for classes to implement.

interface Animal {

    void sound();

}

 

class Cat implements Animal {

    @Override

    public void sound() {

        System.out.println("Meow");

    }

}

 

class Dog implements Animal {

    @Override

    public void sound() {

        System.out.println("Bark");

    }

}

 

public class Main {

    public static void main(String[] args) {

        Animal cat = new Cat();

        Animal dog = new Dog();

        cat.sound();

        dog.sound();

    }

}


7. Association, Aggregation, and Composition

Association: Relationship between two independent classes.
Aggregation: Weak ownership (e.g., a team and players).
Composition: Strong ownership (e.g., a house and rooms).

Example of Composition

class Engine {

    void start() {

        System.out.println("Engine is starting.");

    }

}

 

class Car {

    Engine engine = new Engine();

 

    void startCar() {

        engine.start();

        System.out.println("Car is running.");

    }

}

 

public class Main {

    public static void main(String[] args) {

        Car car = new Car();

        car.startCar();

    }

}


These examples are designed to gradually introduce key OOP concepts in Java. Let me know if you want further explanations or additional examples!

 

Comments

Post a Comment

Popular posts from this blog

Ecommerce website MERN Project Overview