What is the concept of “extends” in Java?

In Java, “extends” is a keyword used to create a subclass or derived class of a class. When a class extends another class, it inherits all the properties and methods of the parent class, and can add new properties and methods in the subclass, or override methods of the parent class. Inheritance allows the subclass to reuse the code of the parent class and extend its functionality.

In terms of syntax, inheritance is achieved by using the keyword “extends” followed by the name of the parent class in the declaration of the subclass. For example:

public class ChildClass extends ParentClass {
    // 子类的代码
}

In the above example, ChildClass is a subclass of ParentClass. Inheritance enables ChildClass to access and utilize all the public and protected methods and properties of ParentClass.

bannerAds