Before moving towards the extends keyword, let us first understand what keywords are in Java. A keyword in Java is unique and performs a special set of tasks. These keywords can not be implemented as identifiers when declared in the code. These keywords can not be used as variables, methods, or classes. Some of the keywords in Java are import, return, throw, implement, etc. Among these keywords, one of the frequently utilized keywords is “extend”.
In this write-up, we will elaborate on the working of the “extends” keyword in Java using different examples.
What Does Extends Keyword Do in Java
The “extend” keyword in Java is used to derive one class from another class. It is also described as the inheritance of one class to another in Java. The derived class or the child class contains all the properties/characteristics of its parent/super class. In order to get a clear understanding of the relationship developed between the parent and the child class, the syntax along with the examples of the extends keyword are shown below.
class Parent {
//code for Parent class
}
class Child extends Parent {
//code for Child class
}
Example 1: Basic Implementation of the Java “extend” Keyword
The below code in Java shows the basic implementation of the “extends” keyword.
// Class in Javaclass M1{
int x = 15;
int y = 20;
}// Use the extend keyword
class M2 extends M1 {
void multiply(){
// The result
System.out.println("The Multiplication of both the numbers are :"+(x*y));
}// The main() method of Java
public static void main(String args[]) {
M2 obj = new M2();
obj.multiply();
}
}
In this code block:
- The first class is declared as “M1” along with two integers as “x” and “y” respectively.
- The child class “M2” extends the parent class “M1”.
- The println() method takes the values of the two integers and multiplies these integers using “*”(asterisk).
- An object “obj” is created in the main class of Java.
- The object is called to print the results accordingly.
Output
The output below shows that the multiplication of two integer values “15” and “20” is printed as “300”.

Example 2: Inheritance in Java Using “extends” Keyword
Inheritance in Java implements the “extends” keyword to create a new class from the existing class, as shown in the code below.
//Declare the class
class Student{
float marks1=89;
}
//The child class extends the parent class
class TotalMarks extends Student{
int total=80;
//main() method of Java
public static void main(String args[]){
//Create an object for the child class
TotalMarks tm = new TotalMarks();
//Print the results
System.out.println("The Marks in Maths are:"+tm.marks1);
System.out.println("Total Percentage is:"+tm.total);
}
}
In the above-stated code:
- The parent class is declared as “Student” which contains a float value.
- Next, the child class that is “TotalMarks” extends the parent class “Student”.
- The marks and percentages are displayed on the output screen by utilizing the println() method.
Output
In the output below, the Marks are printed as “89” that were a part of the parent class “Student” and the percentage is printed as “80” declared in the child class.

Example 3: Utilizing the “extends” Keyword in Single Inheritance
When one class inherits another class it is referred to as “Single Inheritance”. The code below implements single inheritance using the “extends” keyword in Java.
class Employee{
void ID(){
System.out.println("The Employee ID is '202' ");
}
}
//Derived class
class Department extends Employee{
void fullname()
{
System.out.println("The Employee name is: 'John' ");}
public static void main(String args[]){
Department d=new Department();
d.ID(); d.fullname();
}
}
In the above block:
- The base class is declared as “Employee”.
- The ID is declared void since it is without the return type.
- The “Department” class is known as the derived class from its base class “Employee”.
- Lastly, the object for the derived class is created and called to print the results.
Output
The output below shows that the Employee ID is printed as 202 and the Employee name is printed as John.

Example 4: Implementing the “extends” Keyword in Multilevel Inheritance
The inheritance of one class from another class using the extends keyword is called “Multilevel Inheritance” in Java. It is also referred to as “a chain of classes”. The code below shows the implementation of the extends keyword while working with multilevel inheritance in Java.
//Parent class
class Employee{
void ID(){
System.out.println("The Employee ID is '202' ");
}
}
//Child class extends the base class
class Department extends Employee{
void fullname()
{
System.out.println("The Employee name is: 'John' ");
}
}//Another level of extension
class Designation extends Department{
void desg()
{
System.out.println("The Designation of the Employee is 'Banker' ");
}
}//Test the code
class TestCode{
public static void main(String args[]){
Designation d = new Designation();
d.ID();
d.fullname();
d.desg();
}
}
In the above code block, the “Department” class inherits the “Employee” class and the “Designation” class inherits the “Department” class using Java’s extends keyword.
Output
The output below shows that all the EmployeeID, name, and Designation are printed on the screen.

Example 5: Implementation of the “extends” Keyword in Final Class
The Final class in Java restricts the user, it can not be modified or changed. It is clear from the keyword “final” that this class can not be inherited. The code below shows the implementation of the final class in Java
final class M1{
int x = 15;
int y = 20;
}
class M2 extends M1 {
void add(){
System.out.println("The Addition of both the numbers are :"+(x+y));
}
public static void main(String args[]) {
M2 object = new M2();
object.add();
}
}
In the above snippet, the first class is declared by utilizing the “final” keyword. When we try to extend the child class “M2” from its parent class “M1”, an error occurs.
Output
The output shows that inheritance is impossible from the class that is declared with the “final” keyword in Java.

This concludes the discussion on the “extends” keyword in Java along with different examples.
Conclusion
The “extends” keyword in Java inherits one class from another. The basic application of the extends keyword is that of inheritance in Java where the child class inherits the parent class. In this Java blog, we have illustrated the implementation of the extends keyword using several use cases.