Extends Keyword in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

The extends keyword in Java is related to inheritance. The extends keyword extends a class (indicates that a class is inherited from another class). When a class needs to inherit the properties and behavior from another class, it has to use the extends keyword. In this way, the class being extended becomes the Parent class and the class that extends the Parent class becomes the Child class.

Which makes it a child class for the parent class from which the features are being inherited.

What is Java extends keyword?

The extends keyword in Java demonstrates inheritance between a superclass and a subclass. The use of extends keyword indicates that a new subclass has inherited the properties and behaviour of the parent class.

We don't assimilate everything from the basics and try to build the things around us from scratch. There are skills that we acquire from our elders. What we do ahead is acquiring knowledge and create something new on top of it. 

And over the years, we have also seen electronic devices inheriting the existing features from their previous models with an assortment of new functionalities to make them better. This is how inheritance works.

As a real-life example, we know that Java is used for various purposes in the real world. Although it's the same language at the basic level, the varied usage in different fields adds more functionalities on top of the implementation in Java. Be it Web Development, App Development, Software Testing, and also Big Data, Java is present everywhere. In other words, all these various domains inherit the Java programming language and show inheritance. 

Java extends keyword diagram

Java is the Parent class, whereas these domains are the Child classes.But when the discussion here is about Java, how can we show inheritance between two classes?

It can be done with the extends keyword. The extends keyword appended with the new class declaration, followed by the parent class name, shows that the class is inheriting the functionalities of the parent class and will extend it by adding new functionalities in addition to the existing ones.

Syntax of extends Keyword in Java

While applying inheritance in Java, the syntax with the use of extends keyword will be as follows:

Syntax of extends keyword in Java

Let us know in brief about the two classes mentioned above.

  • Base class: Also known as a superclass or Parent class, it is the class whose properties and attributes will be inherited by a child class.
  • Derived class: Also known as subclass or Child class, it is the class that inherits the properties from the Parent class with the help of the extends keyword.

Example

A simple example for extends keyword in Java would be to create a class Vehicle and show inheritance by creating subclasses CycleCar, and Scooter. We know that each of these child classes' names is actually a vehicle at the basic level. The inheritance among the Parent and child classes is an example of IS-A relationship.

Example of extends keyword in Java

Output:

Extending Final Class

If a class has been declared as final, then no subclass can be created further.

Once a class is declared as final, then it cannot be inherited further. In other words, you cannot create a subclass of any final class.

The final keyword is the indication that it's the dead end and you cannot inherit a class that's been declared final. If you try to create a subclass for a final class, it leads to a compilation error.

Then, what's the use of creating a final class?

Well, it can either prevent inheritance or can also help you to create an immutable class so that when an object is created, you cannot change the value of its properties.

To make a class immutable, follow these steps:

  • Declare the class as final.
  • Declare all the data members as private so that they cannot be accessed outside their own class. Later, also declare these data members as final to prevent any modification.
  • There should be no setter methods available because we don't want the fields to get modified.

Let us look at an example where we deliberately try to create a subclass for a final class, which will, in turn, yield a compilation error.

Output:

And indeed, extending a final class didn't work. But yes, if we remove the final keyword from the class A declared in the above example, then we can avoid the compilation error and get the desired output.

Extending Interfaces

The use of interfaces can provide abstraction and also multiple inheritance. Multiple inheritance means that a newly created child class can inherit features from one or more Parent classes.

Extending Interfaces

Normally, you cannot obtain multiple inheritance in Java simply by using classes. However, it can be achieved with the help of interfaces.

So, let us create interfaces for the above example for vehicles and cars particularly. These interfaces will have methods declared without any implementation (without any curly braces). We'll have a Parent interfaceVehicle and a Child interface Car that will inherit the methods from its parent interface.

Conclusion

  • The extends keyword in Java shows inheritance between a superclass and subclass. It indicates that a new subclass has inherited the properties and behaviour of the parent class.
  • The syntax will be:
  • A class that uses the extends keyword will be called a child class or subclass while a class that is extended will become a parent class or superclass.
  • A class declared as final cannot be inherited, so there's no significance in using extends keyword to inherit the properties from a final class.
  • We can achieve multiple inheritance by using extends keyword on interfaces, and the methods will be inherited from a parent interface.