Java Inheritance
The process by which one class acquires the properties (data members) and
functionalities (methods) of another class is called inheritance. The aim of
inheritance is to provide the reusability of code so that a class has to write only
the unique features and rest of the common properties and functionalities can
be extended from another class.
Child Class
The class that extends the features of another class is known as child class, sub
class or derived class.
Parent Class
The class whose properties and functionalities are used(inherited) by another
class is known as parent class, super class or Base class.
To inherit a class we use extends keyword. Here class X is child class and class
A is a parent class. The class X is inheriting the properties and methods of A
class.
class X extends A
{
}
In the example below, the Car class (subclass) inherits the attributes and
methods from the Vehicle class (superclass):
Java code to define the above classes
class Vehicle {//base class
protected String brand = "Ford"; // Vehicle attribute
public void honk() { // Vehicle method
System.out.println("Tuut, tuut!");
class Car extends Vehicle {// Defining a specialized vehicle named
Car
private String modelName = "Mustang"; // Car attribute
Now the Car class have two attributes and one method
public class TestVehicle{//Driver class
public static void main(String[] args) {
// Create a Car object
Car myCar = new Car();
// Call the honk() method (from the Vehicle class) on the myCar
object
myCar.honk();
// Display the value of the brand attribute (from the Vehicle
class) and the value of the modelName from the Car class
System.out.println(myCar.brand + " " + myCar.modelName);
Another example
class Teacher {
protected String designation;
protected String collegeName;
public void setData(String s1, String s2)
Designation = s1; collegeName = s2;
public void does(){
System.out.println("Teaching");
class PhysicsTeacher extends Teacher{
private String mainSubject;
public void display()
System.out.println(“Designation is” + designation + “\n” +
“Main subject is” + mainSubject + “\n”
+
“College is” + collegeName);
public void setData2(String s1, String s2, String s3)
Designation = s1; collegeName = s2; mainSubject = s3;
public class TestTeacher{//Driver class
public static void main(String[] args) {
// Create a Teacher object
Teacher tch = new Teacher();
tch.setData(“Teacher”, “Royal College”);
// Call the does() method (from the teacher class)
tch.does();
// Create a PhysicsTeacher object
PhysicsTeacher ptch = new PhysicsTeacher();
ptch.setData2(“Teacher”, “Royal College”, “Chemistry”);
// Call the does() method (from the PhysicsTeacher class)
ptch.does();
// Call the display() method (from the PhysicsTeacher class)
ptch.display();
Teacher class family with constructors
class Teacher {
protected String designation;
protected String collegeName;
public Teacher (String s1, String s2)
Designation = s1; collegeName = s2;
public void does(){
System.out.println("Teaching");
class PhysicsTeacher extends Teacher{
private String mainSubject;
public void display()
{
System.out.println(“Designation is” + designation + “\n” +
“Main subject is” + mainSubject + “\n”
+
“College is” + collegeName);
}
public PhysicsTeacher (String s1, String s2, String s3)
designation = s1; collegeName = s2; mainSubject = s3;
//or super(s1, s2); mainSubject = s3;
public class TestTeacher{//Driver class
public static void main(String[] args) {
// Create a Teacher object
Teacher tch = new Teacher(“Teacher”, “Royal College”);
// Call the does() method (from the teacher class)
tch.does();
// Create a PhysicsTeacher object
PhysicsTeacher ptch = new PhysicsTeacher(“Teacher”, “Royal
College”, “Chemistry”);
// Call the does() method (from the PhysicsTeacher class)
ptch.does();
// Call the display() method (from the PhysicsTeacher class)
ptch.display();