Inheritance and Polymorphism

Inheritance and polymorphism are two key concepts in object-oriented programming (OOP) that facilitate code reuse, extensibility, and flexibility. Let's explore these concepts in Java:


Inheritance

Inheritance is the mechanism by which one class (subclass or derived class) inherits the properties and behaviors of another class (superclass or base class). It promotes code reuse by allowing subclasses to inherit fields and methods from their superclass.


Syntax

public class Superclass {
    // Fields and methods
}

public class Subclass extends Superclass {
    // Additional fields and methods
}


Example

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

public class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Dog barks.");
    }
}

public class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Cat meows.");
    }
}


Polymorphism

Polymorphism is the ability of an object to take on different forms. In Java, polymorphism is achieved through method overriding and method overloading.


Method Overriding

Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. It allows objects of different classes to be treated as objects of a common superclass.

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

public class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Dog barks.");
    }
}

public class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Cat meows.");
    }
}


Method Overloading

Method overloading occurs when a class has multiple methods with the same name but different parameter lists. It allows methods to behave differently based on the number or type of parameters passed to them.

public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }

    public double add(double a, double b) {
        return a + b;
    }
}


Polymorphic Behavior

Animal animal1 = new Dog();
Animal animal2 = new Cat();

animal1.makeSound(); // Outputs: Dog barks.
animal2.makeSound(); // Outputs: Cat meows.


Conclusion

Inheritance and polymorphism are powerful concepts in Java that enable code reuse, extensibility, and flexibility in object-oriented programming. Inheritance allows subclasses to inherit fields and methods from their superclass, while polymorphism allows objects to take on different forms through method overriding and method overloading. Understanding and applying these concepts effectively can lead to well-designed and maintainable Java applications.

Nenhum comentário:

Postar um comentário

Internet of Things (IoT) and Embedded Systems

The  Internet of Things (IoT)  and  Embedded Systems  are interconnected technologies that play a pivotal role in modern digital innovation....