schroeder.angela12
schroeder.angela12 2d ago β€’ 0 views

How to create a subclass in Java with `extends`

Hey everyone! πŸ‘‹ I'm learning about subclasses in Java using `extends`. Can anyone explain it simply, maybe with a real-world example? I'm a bit confused about how it all works. πŸ€”
πŸ’» Computer Science & Technology

1 Answers

βœ… Best Answer
User Avatar
natasha176 Dec 31, 2025

πŸ“š What is a Subclass in Java?

In Java, a subclass (also known as a derived class or child class) inherits properties and behaviors from another class, known as the superclass (or base class or parent class). The extends keyword is used to establish this inheritance relationship. Think of it like a family tree; the child inherits traits from the parent.

πŸ“œ History and Background

The concept of inheritance, and therefore subclasses, is a fundamental principle of object-oriented programming (OOP). It originated in Simula 67 and Smalltalk in the 1960s and 1970s, aiming to promote code reuse and modularity. Java, being an OOP language, fully embraces inheritance as a core feature.

πŸ”‘ Key Principles of Subclassing

  • 🌿 Inheritance: A subclass inherits all accessible (public, protected, and package-private within the same package) members (fields and methods) from its superclass.
  • ✨ Specialization: Subclasses specialize the superclass by adding new members or overriding existing ones.
  • ⬆️ Polymorphism: Objects of a subclass can be treated as objects of its superclass (is-a relationship). This is a core concept of polymorphism.
  • 🧱 Code Reusability: Inheritance promotes code reusability by allowing subclasses to reuse the code already written in the superclass.

πŸ’» Creating a Subclass with extends: A Practical Example

Let's illustrate subclass creation with a real-world example of animals:


// Superclass
class Animal {
    String name;

    public Animal(String name) {
        this.name = name;
    }

    public void makeSound() {
        System.out.println("Generic animal sound");
    }
}

// Subclass inheriting from Animal
class Dog extends Animal {
    public Dog(String name) {
        super(name); // Call the superclass constructor
    }

    @Override
    public void makeSound() {
        System.out.println("Woof!"); // Override the makeSound method
    }

    public void fetch() {
        System.out.println("Fetching the ball!"); // Additional behavior
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myAnimal = new Animal("Generic Animal");
        Dog myDog = new Dog("Buddy");

        myAnimal.makeSound(); // Output: Generic animal sound
        myDog.makeSound();    // Output: Woof!
        myDog.fetch();      // Output: Fetching the ball!
    }
}

In this example:

  • 🐢 The Dog class extends the Animal class, making Dog a subclass of Animal.
  • 🧬 The Dog class inherits the name attribute from Animal.
  • πŸ”Š The Dog class overrides the makeSound() method to provide a specific implementation for dogs. This is method overriding.
  • 🎾 The Dog class adds a new method called fetch(), which is specific to dogs.
  • super(name) calls the constructor of the superclass (Animal) to initialize the name attribute.

πŸ€” Key Considerations

  • πŸ”’ Access Modifiers: The private members of the superclass are not directly accessible in the subclass. However, they can be accessed indirectly through public or protected methods.
  • 🧱 Constructors: Subclass constructors must call the superclass constructor (using super()) as the first statement, especially when the superclass constructor requires parameters. If the superclass has a no-argument constructor, the call to `super()` is implicit if you don't provide one.
  • 🚫 Multiple Inheritance: Java does not support multiple inheritance of classes (a class cannot extend multiple classes directly). However, a class can implement multiple interfaces.

πŸ“ˆ Benefits of Using Subclasses

  • ♻️ Code Reusability: Avoid writing the same code multiple times.
  • 🧩 Modularity: Organize your code into logical units.
  • πŸ’ͺ Maintainability: Easier to modify and maintain code.
  • 🌐 Extensibility: Easier to add new functionality without modifying existing code.

πŸŽ“ Conclusion

Creating subclasses with extends is a crucial part of object-oriented programming in Java. It enables code reuse, promotes modularity, and facilitates the creation of complex and maintainable software. By understanding the principles of inheritance and practicing with real-world examples, you can effectively leverage subclasses to build robust and scalable applications.

Join the discussion

Please log in to post your answer.

Log In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! πŸš€