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
10 simple ways to add two numbers in Java In this article, we will discuss…
Write a Python program to find the first n prime numbers In this article we…
Python: Calculate Average of odd and even in a list using loops In this post,…
Python: Average of Odd & Even Numbers from User Input In this post, we will…
Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…
Python program to calculate the sum of odd and even numbers in a list In…