Categories: Keyword in Java

The extends keyword in Java programming language

The extends keyword in Java programming language

We will learn in this tutorial about The extends keyword in Java programming language

extends is a keyword in Java language, which is used to inheritance process to inherit the property of the class (from the parent class to child class),like this

Usage of extends keyword in Java
class A extends B{..........}

newly creating class or derived class use extends keyword to extend  data members or methods of  already exiting class or base class(inherit extra attributes).

Private variable and methods can not be inherit extends keyword

How to works extends keyword

public class Base_class{ 
//Data members(variable and methods)
}

This is a base class or parent class

 

public class Derived_class extends Base_class{
//child class inherits properties of base class
}

This is derived class inherit properties of Base class using extends keyword

 

Example

Program

public class A{
public int number1;
public int number2;
}

class B extends A{
public static add(){
add1=number1+number2
}
}

In this example, we inherit properties from class A, which means that class B will also contain a method add 1. Class B inherits properties from class A as number 1 and number 2 for calculation.

Example

class A{
public static int num1=45;
public static int num2=56;

}
class B extends A{
    void add(){
    System.out.println("Total of num1,num2 :"+(num1+num2)); 
    }
    public static void main(String args[]){
        B obj=new B();
        obj.add();
    }
    
}

 

When the above code is compiled and executed, it will produce the following result

Addition of num1,num2 :101

 

There are other Java language keywords that are similar to this keyword

implements keyword in java

public Key words in Java

static keyword in Java language

Volatile Keyword in Java

Transient keyword in Java

Package keyword in Java

class keyword in Java

boolean keyword in Java

break keyword in Java

do keyword in Java

double keyword in Java

This keyword in Java

final keyword in Java

switch keyword in Java

interface keyword in java

 

Suggested for you

 Java language Keywords

C language Keywords

 Python language Keywords

keywords in C language

Usage of final keyword in Java

Usage of this keyword in Java

 

 

 

 

 

The finally keyword in Java programming language
The implements keyword in Java language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago