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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

6 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

6 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

6 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

9 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

1 year ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

1 year ago