Inheritance:
Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.
The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).
extends Keyword
extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.
class Super{ ..... ..... } class Sub extends Super{ ..... ..... }
Sample Code
Below given is an example demonstrating Java inheritance. In this example you can observe two classes namely Calculation and My_Calculation.
Using extends keyword the My_Calculation inherits the methods addition() and Subtraction() of Calculation class.
Copy and paste the program given below in a file with name My_Calculation.java
class Calculation{ int z; public void addition(int x, int y){ z=x+y; System.out.println("The sum of the given numbers:"+z); } public void Substraction(int x,int y){ z=x-y; System.out.println("The difference between the given numbers:"+z); } } public class My_Calculation extends Calculation{ public void multiplication(int x, int y){ z=x*y; System.out.println("The product of the given numbers:"+z); } public static void main(String args[]){ int a=20, b=10; My_Calculation demo = new My_Calculation(); demo.addition(a, b); demo.Substraction(a, b); demo.multiplication(a, b); } }