Inheritance allows one class to reuse the functionality provided by its superclasses. The extends clause in a class declaration establishes an inheritance relationship between two classes.
Note that A class may directly extend only one superclass. Each of those subclasses may itself have several subclasses.
Syntax of class to Inherit (Inheritance)
class clsName2 extends clsName1
{
// class body
}
clsName2 is subclass of clsName1 or clsName1 is a superclass of clsName2.
There is an important feature of java that relates to class hierarchies and reference variables. To declare a variable that references an object is as below.
Syntax of Variable declaration
clsName varName;
varName is a name of the variable and clsName is a name of its class. So, varName can reference any object of class clsName. Also note that varName can also reference any object whose class is a subclass of clsName.
For example,
There is a superclass "Person" have having subclasses "Employee" and "Teacher". "Employee" class have subclasses "Permanent Employee" and "Contract Employee".
As per above example Person class reference can hold of its sub classes i.e. Employee, teacher, Permanent Employee or Contract class object.
Note that above feature is an important feature of java to implement run-time polymorphism.
Example of Inheritance or Superclass - Subclass Concept
Example : Program that illustrates inheritance in java using person class
class Person
{
String FirstName;
String LastName;
Person(String fName, String lName)
{
FirstName = fName;
LastName = lName;
}
void Display()
{
System.out.println("First Name : " + FirstName);
System.out.println("Last Name : " + LastName);
}
}
class Student extends Person
{
int id;
String standard;
String instructor;
Student(String fName, String lName, int nId, String stnd, String instr)
{
super(fName,lName);
id = nId;
standard = stnd;
instructor = instr;
}
void Display()
{
super.Display();
System.out.println("ID : " + id);
System.out.println("Standard : " + standard);
System.out.println("Instructor : " + instructor);
}
}
class Teacher extends Person
{
String mainSubject;
int salary;
String type; // Primary or Secondary School teacher
Teacher(String fName, String lName, String sub, int slry, String sType)
{
super(fName,lName);
mainSubject = sub;
salary = slry;
type = sType;
}
void Display()
{
super.Display();
System.out.println("Main Subject : " + mainSubject);
System.out.println("Salary : " + salary);
System.out.println("Type : " + type);
}
}
class InheritanceDemo
{
public static void main(String args[])
{
Person pObj = new Person("Rayan","Miller");
Student sObj = new Student("Jacob","Smith",1,"1 - B","Roma");
Teacher tObj = new Teacher("Daniel","Martin","English","6000","Primary Teacher");
System.out.println("Person :");
pObj.Display();
System.out.println("Student :");
sObj.Display();
System.out.println("Teacher :");
tObj.Display();
}
}
Output
Person :
First Name : Rayan
Last Name : Miller
Student :
First Name : Jacob
Last Name : Smith
ID : 1
Standard : 1 - B
Instructor : Roma
Teacher :
First Name : Daniel
Last Name : Martin
Main Subject : English
Salary : 6000
Type : Primary Teacher