Java

Java Extends Keyword

“The keyword “extends” in Java is used for inheritance. It is used to declare that the class and interface of the child class are inherited from the class and interface of the parameter class. The parent class in Java, known as the superclass, is the only class we can extend. The extended keyword passed down the attributes and the features of the parent class to the new child classes. This implies that we can make use of the parent class’s properties in the child classes. The Java extends keyword allows the classes to provide unique features and enhance the reusability of the code.”

Example 1

The class can only be extended when the “extends” modifier is used with it. Then, only the class can access the fields and properties of another class. The following code snippet has a basic example for demonstrating the extend keyword.

Here, we have created the parent class “Manager”. In the parent class “Manager”, we have set the variables “status” and “organizationName”. These variables contain string values against each variable individually. After that, we defined the “parentClass()” function of the parent class “Manager”, where we printed the statement.

Next, we have declared the child class “ExtendClassExample1”, which uses the “extend” keyword for the parent class “Manager”. Now, we access the specified variables and function of the parent class “Manager” in this child class. We have created a variable “Department” of the child class “ExtendClassExample1” and assigned the value to it. Then, we created the main() method to execute the code. We have called the child class there and set the object “MyObj” for this child class.

With the object “MyObj” of the child class “ExtendClassExample1”, we have accessed the variables and the function “parentClass()”. The values that the variables and the function hold of the parent class “Manager” will be printed on the screen.

The properties of the parent class are displayed on the console, which we have obtained from the child class. The “extends” keyword generates the relationship between the parent class and the child class.

Example 2

The “extends” keyword cannot be deployed with the class that is defined with the final keyword, as we cannot create the child class of the final class. Once the final class is constructed, then inheritance is not possible because it makes the class immutable. Below, we have done the extended operation of the final class to know how the error message occurred.

Here, we have defined a class “First” with the modifier “final”. We have created the “Show()” function inside the final class and printed the string message. Next, we have declared a class “Second”, which is the subclass of the final class “First” by using the “extends” keyword. We have called the same function “show()” of the final class “First” and passed another print statement to it. Then, we constructed another class, “ExtendClassExample2”, for the main() method, which executes the methods of another class. We have declared the object “obj1” for the final class “First” and also defined the object “obj2” for the subclass “second”.

When the above program is executed, the error is thrown that the final class cannot be inherited in Java.

Example 3

We can use the chain of the “extends” keyword with the classes. This is known as java multiple inheritances, where a Derived class extends from the Base class, and that Derived class becomes a Base class for another Derived class.

Here, we have established a base class, “Family”, which is set with the function parent(). The parent() function is defined with the message that will be displayed via the print() method. Then, we created a derived class “Child1” from the base class “Family” by deploying the “extend” modifier in between the class.

There, we have also declared a function “Son”, which has the print message. Next, we have specified another class, “Child2”, which is derived from the class “Child1” by the keyword “extend”. This “child1” class is now the base class for “child2”. We have called a function “grandson()” inside the class, which also displayed the string statement. Next, we have a main class, “ExtendClassExample3”, of the program because the main() function is defined inside it. We have created the object “obj” for “Child2” and called the functions from the base classes.

The statements of the base classes and the derived class are input into the console. This way, we can perform multiple inheritances while utilizing the “extend” modifier.

Example 4

The keyword “extends” is also used for hierarchical inheritance. When the subclasses are extended from only one superclass, that case is denoted as hierarchical inheritance.

Here, we have a superclass, “Teacher”, which has a “Teacher_Info” function. Then, we have defined three subclasses, “student1”, “student2”, and “student3”, which are also specified with the functions, and the “extends” keyword extends these from the superclass “Teacher”. Next, we have a driver class, “ExtendClassCase4”, which has the main() method. We have declared the objects “S1_Obj”, “S2_Obj”, and “S3_Obj” of each subclass, and each subclass is called the function “Teacher_Info” from the superclass.

The functions statement of each subclass is displayed on the console with the function statement of the superclass that the subclasses have called.

Example 5

The extended class, which is formed by using the “extends” modifier, can also override the operations of the superclass. In the following program, we performed the override operation in the superclass function.

Here, we have formed a superclass, “Fruit”, and defined a “juice()” function inside it. The function “juice()” has a print string. Then, we extended the superclass “Fruit” by creating the subclass “Apple” with the modifier “extends”. We have invoked the superclass “Fruit” function “juice()” inside it by specifying the “@override” keyword and modifying the string with the new string message.

Moreover, we have a new function, “drinks(),” of the subclass, which also contains the string message. The object “a” is declared for the subclass “Apple”, which invoked its functions.

Hence, we have overridden the statement of the superclass function inside the subclass, which is shown here as output.

Conclusion

The “extends” keyword is used by the derived class for extending the base class. The “extends” keyword demonstrates inheritance between the base class and derived classes. We have extended the subclasses from the superclass with the “extends” modifier in the above examples. We have achieved the interfaces of the multiple and hierarchical inheritance by employing the “extend” keyword.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.