Object-Oriented Programming in Java

Object-Oriented Programming (OOP) is a paradigm that models real-world entities using classes and objects. Java is a fully object-oriented language, making OOP concepts crucial for developers. Let’s explore the basics.

Classes and Objects

A class is a blueprint for creating objects, while an object is an instance of a class. Here’s an example:

public class Car {
        // Fields (attributes)
        String brand;
        int speed;

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

        // Method (behavior)
        public void displayInfo() {
            System.out.println("Brand: " + brand + ", Speed: " + speed + " km/h");
        }
    }

    public class Main {
        public static void main(String[] args) {
            // Create objects
            Car car1 = new Car("Toyota", 180);
            Car car2 = new Car("BMW", 220);

            // Call methods
            car1.displayInfo(); // Output: Brand: Toyota, Speed: 180 km/h
            car2.displayInfo(); // Output: Brand: BMW, Speed: 220 km/h
        }
    }

Encapsulation

Encapsulation hides the implementation details and allows controlled access using getters and setters:

public class Person {
        private String name;

        // Getter
        public String getName() {
            return name;
        }

        // Setter
        public void setName(String name) {
            this.name = name;
        }
    }

    public class Main {
        public static void main(String[] args) {
            Person person = new Person();
            person.setName("Alice");
            System.out.println("Name: " + person.getName()); // Output: Name: Alice
        }
    }

Inheritance

Inheritance allows one class to inherit fields and methods from another class:

class Animal {
        void eat() {
            System.out.println("This animal eats food.");
        }
    }

    class Dog extends Animal {
        void bark() {
            System.out.println("This dog barks.");
        }
    }

    public class Main {
        public static void main(String[] args) {
            Dog dog = new Dog();
            dog.eat(); // Output: This animal eats food.
            dog.bark(); // Output: This dog barks.
        }
    }

Polymorphism

Polymorphism allows a single interface to represent different behaviors:

class Animal {
        void makeSound() {
            System.out.println("This animal makes a sound.");
        }
    }

    class Cat extends Animal {
        void makeSound() {
            System.out.println("Meow");
        }
    }

    public class Main {
        public static void main(String[] args) {
            Animal myAnimal = new Cat();
            myAnimal.makeSound(); // Output: Meow
        }
    }

Next Steps

Explore OOP concepts like abstraction, interfaces, and design patterns. OOP is the foundation of Java programming and is essential for building robust applications.