Inheritance makes code reusability. A class can inherit functions and attributes from another class. The class from which it is inherited is called the superclass and the inherited class is called the subclass. Keyword extends is used for inheritance.
The following program shows inheritance from superclass First to subclass Second.
package com.company;
public class First {
protected int x = 3;
protected void PrintNumber() {
System.out.println("The number is "+x);
}
}
class Second extends First {
private int y = 4;
public static void main(String[] args) {
Second obj = new Second();
obj.PrintNumber();
System.out.println("The numbers are "+obj.x+" and "+obj.y);
}
}