OOP Concepts

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects, which can contain data (fields or attributes) and code (methods). Java, being an OOP language, relies heavily on these principles. The core OOP concepts are:

1. Encapsulation

  • Definition: Encapsulation is the bundling of data (variables) and methods (functions) that operate on the data into a single unit, usually a class. It restricts direct access to some components of an object to ensure controlled access.

  • Key Points:

    • Achieved using access modifiers like private, protected, and public.

    • Provides getter and setter methods to access and update private fields.

  • Example:

    public class Person {
        private String name; // Encapsulated field
    
        // Getter
        public String getName() {
            return name;
        }
    
        // Setter
        public void setName(String name) {
            this.name = name;
        }
    }
    

2. Inheritance

  • Definition: Inheritance allows a class (child or subclass) to inherit properties and behaviors (fields and methods) from another class (parent or superclass). It enables code reuse and establishes a hierarchy.

  • Key Points:

    • Use the extends keyword in Java.

    • Supports single inheritance (one class can inherit from one superclass).

  • Example:

    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(); // Inherited method
            dog.bark(); // Dog's own method
        }
    }
    

3. Polymorphism

  • Definition: Polymorphism allows objects to take on many forms. A single method or operator can perform different tasks depending on the context.

  • Types:

    • Compile-Time Polymorphism (Method Overloading): Same method name with different parameters.

    • Runtime Polymorphism (Method Overriding): Subclass provides a specific implementation of a method already defined in its parent class.

  • Example:

    // Method Overloading
    class Calculator {
        int add(int a, int b) {
            return a + b;
        }
    
        double add(double a, double b) {
            return a + b;
        }
    }
    
    // Method Overriding
    class Animal {
        void sound() {
            System.out.println("This animal makes a sound.");
        }
    }
    
    class Cat extends Animal {
        @Override
        void sound() {
            System.out.println("The cat meows.");
        }
    }
    

4. Abstraction

  • Definition: Abstraction hides the implementation details and only exposes the functionality. It focuses on "what" an object does rather than "how" it does it.

  • Key Points:

    • Achieved using abstract classes and interfaces in Java.

    • Abstract classes can have both concrete methods and abstract methods (methods without implementation).

    • Interfaces define only abstract methods (before Java 8) or default/static methods (Java 8 and later).

  • Example:

    // Abstract Class
    abstract class Shape {
        abstract void draw();
    }
    
    class Circle extends Shape {
        @Override
        void draw() {
            System.out.println("Drawing a Circle.");
        }
    }
    
    // Interface
    interface Vehicle {
        void start();
    }
    
    class Car implements Vehicle {
        @Override
        public void start() {
            System.out.println("Car starts.");
        }
    }
    

These four pillars—Encapsulation, Inheritance, Polymorphism, and Abstraction—form the foundation of object-oriented programming in Java.