OOP and Key Concepts of OOP

Object Oriented Programming and Key Concepts of OOP

Object-oriented programming (OOP) is a programming paradigm that uses objects and their interactions to design applications and computer programs. OOP is a powerful and versatile approach to programming that can be used to create a wide range of applications, from simple pieces of code to complex enterprise systems.

Four pillars of OOP are

  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction

Encapsulation

Encapsulation is the concept of bundling data (attributes) and methods (functions) that operate on that data into a single unit called a class. It restricts access to some of an object’s components, only exposing what is necessary and relevant. This helps in data hiding, preventing unauthorized access, and maintaining the integrity of an object’s state.

Inheritance

Inheritance is a mechanism that allows one class (the subclass or derived class) to inherit the properties and methods of another class (the superclass or base class). It promotes code reusability and the creation of a hierarchy of classes. Subclasses can add their own features while inheriting the characteristics of their parent class.

Polymorphism

Polymorphism means “many forms.” It allows objects of different classes to be treated as objects of a common superclass. Polymorphism enables method overriding, where a subclass provides its own implementation of a method defined in the superclass. This allows for dynamic method dispatch, making it possible to write more flexible and extensible code.

Abstraction

Abstraction is the process of simplifying complex reality by modeling classes based on the essential properties and behaviors of objects. It focuses on what an object does rather than how it does it. Abstraction helps in managing complexity and providing a clear, high-level view of the system’s functionality.

Following example shows the use case of above mentioned four pillars of Object Oriented Programming.

// Pillar 1: Encapsulation
class Person {
    private String name;
    private int age;

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

    public void displayDetails() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }
}

// Pillar 2: Inheritance
class Student extends Person {
    private String studentId;

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

    public void displayStudentDetails() {
        super.displayDetails();
        System.out.println("Student ID: " + studentId);
    }
}

// Pillar 3: Polymorphism
class Teacher extends Person {
    private String employeeId;

    public Teacher(String name, int age, String employeeId) {
        super(name, age);
        this.employeeId = employeeId;
    }

    @Override
    public void displayDetails() {
        System.out.println("Teacher Information:");
        super.displayDetails();
        System.out.println("Employee ID: " + employeeId);
    }
}

// Pillar 4: Abstraction (not explicitly shown in the code, but it's inherent)
public class Main {
    public static void main(String[] args) {
        Student student = new Student("Alice", 20, "S12345");
        Teacher teacher = new Teacher("Mr. Smith", 35, "T9876");

        // Encapsulation and Inheritance
        student.displayStudentDetails();
        System.out.println();
        teacher.displayDetails();
    }
}

The Person class encapsulates the name and age attributes and provides a public method displayDetails() to access them. These attributes are private, ensuring data hiding and controlled access.

The Student and Teacher classes inherit from the Person class. They reuse the attributes and methods defined in Person while adding their own unique attributes and behaviours.

The Teacher class overrides the displayDetails() method from the Person class to provide a specialised implementation. This demonstrates method overriding and polymorphic behaviour.

Abstraction is inherent in the design of classes like Person, Student, and Teacher, as they model real-world entities and abstract away unnecessary details. The use of methods like displayDetails() allows us to work with objects at a higher level of abstraction.

Let’s discuss deeply about this Encapsulation, Inheritance, Polymorphism and Abstraction in the next articles. Stay tuned.

Leave a Reply

Your email address will not be published. Required fields are marked *