The extends keyword in Java programming language
- Home
- Keyword in Java
- The extends keyword in Java programming language
- On
- By
- 0 Comment
- 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
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
static keyword in Java language
Suggested for you
Usage of final keyword in Java