T E C h O C E A N H U B

Encapsulation, inheritance, and polymorphism

In Java, encapsulation, inheritance, and polymorphism are three fundamental concepts of object-oriented programming. They play a crucial role in designing and implementing robust and flexible software systems. Let’s discuss each of these concepts individually:

  1. Encapsulation: Encapsulation is the process of bundling data (variables) and methods (functions) that manipulate that data within a single unit, called a class. It allows the class to control the access to its internal state, providing a way to protect data from unwanted modification or direct access by other classes. Encapsulation promotes data hiding and abstraction, which helps in achieving better code maintainability and reusability.

To encapsulate data in Java, you typically declare private instance variables in a class and provide public getter and setter methods (also known as accessors and mutators) to interact with the data. By controlling the access to the data through these methods, you can enforce validation, perform actions upon data access, or change the internal representation without affecting other parts of the code.

Encapsulation example:

public class Person {
    private String name;
    private int age;
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public int getAge() {
        return age;
    }
    
    public void setAge(int age) {
        if (age >= 0) {
            this.age = age;
        }
    }
}

In the above example, the Person class encapsulates the data fields name and age by declaring them as private. It provides public getter and setter methods (getName(), setName(), getAge(), setAge()) to access and modify the private fields. The encapsulation allows controlled access to the internal state of the Person object, ensuring data integrity and providing abstraction.

  1. Inheritance: Inheritance is a mechanism that allows one class to inherit the properties (fields and methods) of another class. The class that inherits is called the subclass or derived class, while the class being inherited from is called the superclass or base class. Inheritance facilitates code reuse and establishes an “is-a” relationship between classes.

In Java, you can create a subclass by using the extends keyword followed by the name of the superclass in the class declaration. The subclass inherits all the accessible members (variables and methods) of the superclass and can add its own specific members or override the inherited ones. Through inheritance, you can create a hierarchy of classes, with each level of the hierarchy inheriting and extending the behavior of the classes above it.

Inheritance example:

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...");
    }
}

In the above example, the Animal class serves as the superclass, and the Dog class extends it, making Dog the subclass. The Dog class inherits the eat() method from the Animal class. The Dog class also adds its own method bark(). With inheritance, the Dog class inherits the behavior of the Animal class and can extend it further.

  1. Polymorphism: Polymorphism is the ability of an object to take on different forms or behaviors. In Java, polymorphism allows objects of different classes to be treated as objects of a common superclass or interface. This provides flexibility and extensibility in designing and working with complex systems.

There are two types of polymorphism in Java: compile-time (static) polymorphism and runtime (dynamic) polymorphism.

  • Compile-time polymorphism is achieved through method overloading, where multiple methods with the same name but different parameter lists can be defined within a class. The appropriate method to be called is determined at compile-time based on the arguments provided.
  • Runtime polymorphism is achieved through method overriding, where a subclass provides its own implementation of a method that is already defined in its superclass. The method to be executed is determined at runtime based on the actual type of the object.

Polymorphism enables you to write more generic code that can work with objects of different types, as long as they adhere to a common interface or share a superclass.

Polymorphism example:

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

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

public class Rectangle extends Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a rectangle");
    }
}

public class PolymorphismExample {
    public static void main(String[] args) {
        Shape shape1 = new Circle();
        Shape shape2 = new Rectangle();
        
        shape1.draw();  // Output: Drawing a circle
        shape2.draw();  // Output: Drawing a rectangle
    }
}

In the above example, the Shape class is a superclass, and the Circle and Rectangle classes are subclasses. Each subclass overrides the draw() method of the superclass with its own implementation. In the PolymorphismExample class, objects of the Circle and Rectangle classes are assigned to variables of type Shape. The draw() method is called on these objects, and at runtime, the appropriate overridden method is executed based on the actual type of the object. This demonstrates runtime polymorphism.

These three concepts, encapsulation, inheritance, and polymorphism, form the foundation of object-oriented programming and are extensively used in Java to build modular, maintainable, and scalable applications.

Copyright ©TechOceanhub All Rights Reserved.