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
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
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
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
static keyword in Java language
Suggested for you
Usage of final keyword in Java
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…