Skip to main content Skip to docs navigation
TechSpiderTutorials

java extends keyword

java.lang.Integer

In Java programming, the extends keyword plays a pivotal role in facilitating one of the core principles of object-oriented programming (OOP) — inheritance. Understanding how extends operates is fundamental for anyone looking to build scalable and maintainable Java applications.

Using the extends Keyword

  // Defining a superclass
public class Animal {
    // Fields and methods
}

// Defining a subclass that extends Animal
public class Dog extends Animal {
    // Additional fields and methods specific to Dog
}

Key Concepts

Single Inheritance: Java supports single inheritance, meaning a subclass can extend only one superclass. However, Java supports multiple levels of inheritance, where a subclass can itself be extended by another subclass.

Access Modifiers: The extends keyword affects the visibility of superclass members in the subclass. Members with public and protected access are inherited and accessible in the subclass, whereas private members are not directly accessible.

Constructors: When a subclass is instantiated, its constructor implicitly calls the constructor of its superclass. If the superclass constructor requires arguments, the subclass constructor must explicitly call it using super().

Overriding and Polymorphism

Subclasses can override superclass methods to provide specific implementations using the @Override annotation. This enables polymorphism, where objects of different classes can be treated as objects of a common superclass.