Overview:

In Lesson 1, we introduce the foundational concepts of Object-Oriented Programming (OOP), a paradigm widely used for designing and organizing code. OOP is centered around four key principles: encapsulation, inheritance, polymorphism, and abstraction. Understanding these principles is essential for creating modular, reusable, and maintainable code.

Key Concepts:

  1. Encapsulation:
    • Definition: Encapsulation is the bundling of data (attributes) and the methods (functions) that operate on the data into a single unit known as a class.
    • Purpose: It helps in hiding the internal details of how an object works and exposing only what is necessary.
// Example of Encapsulation
public class Person {
    private String name;
    private int age;

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

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Inheritance:

// Example of Inheritance
public class Animal {
    public void eat() {
        System.out.println("Animal is eating.");
    }
}

public class Dog extends Animal {
    public void bark() {
        System.out.println("Dog is barking.");
    }
}

Polymorphism:

// Example of Polymorphism
public interface Shape {
    void draw();
}

public class Circle implements Shape {
    public void draw() {
        System.out.println("Drawing a circle.");
    }
}

public class Square implements Shape {
    public void draw() {
        System.out.println("Drawing a square.");
    }
}

Abstraction:

// Example of Abstraction
public abstract class Shape {
    // Abstract method (no implementation)
    public abstract void draw();
}

public class Circle extends Shape {
    public void draw() {
        System.out.println("Drawing a circle.");
    }
}

public class Square extends Shape {
    public void draw() {
        System.out.println("Drawing a square.");
    }
}

Example:

Let’s create a simple program that demonstrates the principles of OOP. We’ll define a Person class using encapsulation, create a Student class that inherits from Person, showcase polymorphism with a displayInfo method, and use abstraction with an abstract Shape class.

// Encapsulation Example
public class Person {
    private String name;
    private int age;

    // Constructor
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

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

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

// Inheritance and Polymorphism Example
public class Student extends Person {
    private int studentId;

    // Constructor
    public Student(String name, int age, int studentId) {
        super(name, age);
        this.studentId = studentId;
    }

    // Polymorphism: Display information
    public void displayInfo() {
        System.out.println("Student ID: " + studentId);
        System.out.println("Name: " + getName());
        System.out.println("Age: " + getAge());
    }
}

// Abstraction Example
public abstract class Shape {
    // Abstract method (no implementation)
    public abstract void draw();
}

// Concrete classes implementing Shape
public class Circle extends Shape {
    public void draw() {
        System.out.println("Drawing a circle.");
    }
}

public class Square extends Shape {
    public void draw() {
        System.out.println("Drawing a square.");
    }
}

This example showcases encapsulation, inheritance, polymorphism, and abstraction, laying the foundation for a deeper exploration of OOP principles in Java.