Java Inheritance

 

Java Inheritance defines as IS-A relationship between a superclass and its subclasses. 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

The idea behind inheritance in java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.

Syntax:

 

class clsName2 extends clsName1
{
      // class body
}

 
clsName2  is subclass of clsName1 or
clsName1 is a superclass of clsName2.

THIS

This keyword is used as a reference to the current object which is an instance of the current class

SUPER

The keyword super also references the current object, but as an instance of the current class’s super class.



Example 1:

•    To print the name, age, ph, specialization and department by using superclass user and the subclass of Employee and manager.

class User
{
    String name;
    int age;
    long ph;
    void salary()
    {
        System.out.println("salary");
    }
}
 class Employee extends User
{
    String specialization;
  }

 

class Manager extends User
{
 String department;
}
class Main
{
    public static void main(String[] args) {
        Employee e1=new Employee();
            e1.name="Candid";
            e1.age=22;
            e1.ph=123456789l;
            e1.specialization="Java";
        Manager m1=new Manager();
            m1.name="java";
            m1.age=25;
            m1.ph=345789l;
            m1.department="HR";
            
            System.out.println(e1.name);
            System.out.println(e1.age);
            System.out.println(e1.ph);
            System.out.println(e1.specialization);
            
            System.out.println(m1.name);
            System.out.println(m1.age);
            System.out.println(m1.ph);
            System.out.println(m1.department);            
    }}


Example 2:

•    To print the addition, subtraction and multiplication by using superclass Calculation and the subclass of My_Calculation
  

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);      
   }
                    }



Compile and execute the above code as shown below

javac My_Calculation.java
java My_Calculation

 After executing the program it will produce the following result.

The sum of the given numbers:30
The difference between the given numbers:10
The product of the given numbers:200



Example 3:
•    To print the Volume, width, height and depth by using superclass Box and the subclass of MatchBox

class Box {

    double width;
    double height;
    double depth;
    Box() {
    }

 

Box(double w, double h, double d) {
        width = w;
        height = h;
        depth = d;
    }

 

void getVolume() {
        System.out.println("Volume is : " + width * height * depth);
    }
}
public class MatchBox extends Box {

    double weight;
    MatchBox() {
    }
MatchBox(double w, double h, double d, double m) {
    super(w, h, d);
        weight = m;
    }
Public class checkVol(){
    public static void main(String args[]) {
        MatchBox mb1 = new MatchBox(10, 10, 10, 10);
        mb1.getVolume();
        System.out.println("width of MatchBox 1 is " + mb1.width);
        System.out.println("height of MatchBox 1 is " + mb1.height);
        System.out.println("depth of MatchBox 1 is " + mb1.depth);
        System.out.println("weight of MatchBox 1 is " + mb1.weight);
    }
}


  Output
Volume is : 1000.0
width of MatchBox 1 is 10.0
height of MatchBox 1 is 10.0
depth of MatchBox 1 is 10.0

 

Example 4:

•    To print the Car number,No of Tyres, accessories, Brand, Number of Vehicles by using superclass Vehicle and the subclass of Car

class Vehicle {
    // Instance fields
    int noOfTyres; // no of tyres
    private boolean accessories; // check if accessorees present or not
    protected String brand; // Brand of the car
    // Static fields
    private static int counter; // No of Vehicle objects created
    // Constructor
Vehicle() {
        System.out.println("Constructor of the Super class called");
        noOfTyres = 5;
        accessories = true;
        brand = "X";
        counter++;
    }
// Instance methods
    public void switchOn() {
        accessories = true;
    }
public void switchOff() {
        accessories = false;
    }
public boolean isPresent() {
        return accessories;
    }
private void getBrand() {
        System.out.println("Vehicle Brand: " + brand);
    }
    // Static methods
    public static void getNoOfVehicles() {
        System.out.println("Number of Vehicles: " + counter);
    }
}
class Car extends Vehicle {
    private int carNo = 10;
    public void printCarInfo() {
        System.out.println("Car number: " + carNo);
        System.out.println("No of Tyres: " + noOfTyres); // Inherited.
        //  System.out.println("accessories:+ accessories); 
                // Not Inherited.
        System.out.println("accessories: " + isPresent()); // Inherited.
        //        System.out.println("Brand: + getBrand());  
                // Not Inherited.
        System.out.println("Brand: " + brand); // Inherited.
        //  System.out.println("Counter:; + counter); // Not Inherited.
        getNoOfVehicles(); // Inherited.
    }
}
public class VehicleDetails { // (3)

    public static void main(String[] args) {
        new Car().printCarInfo();
    }
}



Output

Constructor of the Super class called
Car number: 10
No of Tyres: 5
accessories: true
Brand: X
Number of Vehicles: 1