Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Inheritance and Polymorphism in Java

1. Inheritance

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit properties and methods from another class.

Key Takeaway: Inheritance promotes code reusability.

Key Concepts

  • Base Class (Parent Class): The class being inherited from.
  • Derived Class (Child Class): The class that inherits from the base class.
  • Single Inheritance: A child class inherits from one parent class.
  • Multiple Inheritance: A child class inherits from multiple parent classes (not directly supported in Java).

2. Polymorphism

Polymorphism allows methods to do different things based on the object it is acting upon, even if they share the same name.

Key Takeaway: Polymorphism enhances flexibility in code.

Types of Polymorphism

  • Compile-time Polymorphism (Method Overloading): Same method name with different parameters.
  • Runtime Polymorphism (Method Overriding): Child class method overrides parent class method.

3. Code Examples

Inheritance Example


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

class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        myDog.sound(); // Output: Dog barks
    }
}
                

Polymorphism Example


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

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

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

public class Main {
    public static void main(String[] args) {
        Shape myShape;
        myShape = new Circle();
        myShape.draw(); // Output: Drawing a circle
        myShape = new Square();
        myShape.draw(); // Output: Drawing a square
    }
}
                

4. Best Practices

  • Use inheritance to promote code reusability.
  • Avoid deep inheritance trees; prefer composition over inheritance when possible.
  • Use method overriding judiciously to maintain clarity and avoid confusion.
  • Document your classes and methods to clarify intended behavior in polymorphic contexts.

5. FAQ

What is the difference between inheritance and polymorphism?

Inheritance is a mechanism where one class inherits the properties and methods of another class, while polymorphism allows a method to perform differently based on the object invoking it.

Can we achieve multiple inheritance in Java?

No, Java does not support multiple inheritance with classes. However, it can be achieved through interfaces.

What is method overriding?

Method overriding occurs when a derived class has a method with the same name and signature as a method in its base class, allowing the derived class to provide a specific implementation.