Skip to main content

Introduction to Java Programming

  What is Java? Java is a programming language and a platform. Java is a high-level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Firstly, it was called  Greentalk  and the file extension was  .gt . After that, it was called  Oak . Initially Java was designed for small, embedded systems in electronic appliances like set-top boxes. Platform:  Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API(Application Programming Interface), it is called a platform. The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured, High performance, Multithreaded, Architecture neutral, Object-oriented, Interpreted, and Dynamic". Currently, Java is used in internet programming, mobile devices, g...

16. Inheritance in Java

 

Inheritance in Java

  • In Java, one class can easily inherit the attributes and methods from some other class. This mechanism of acquiring objects and properties from some other class is called as inheritance in Java.
  • Inheritance is used to borrow properties and methods from an existing class, which increases the code's reusability.
  • The keyword used for inheritance is extends.

The different types of inheritance which are supported by Java are as:

Single Inheritance

Sub-class inherit the features of one super-class.

class Parent {
    int y;

    public void setY(int y) {
        System.out.println("Setting y in Parent class...");
        this.y = y;
    }

    public int getY() {
        return this.y;
    }
}

class Child extends Parent { // acquiring property of Parent class
    private int z;

    public void setZ(int z) {
        System.out.println("Setting z in Child class...");
        this.z = z;
    }

    public int getZ() {
        return this.z;
    }
}

class SingleInheritance {
    public static void main(String[] arr) {
        Child obj = new Child(); // Contains properties of Child and Parent classes
        obj.setZ(10);
        System.out.println("Age of child is: " + obj.getZ());
        obj.setY(20);
        System.out.println("Age of parent is: " + obj.getY());
    }
}

Output :

Setting z in Child class...
Age of child is: 10
Setting y in Parent class...
Age of parent is: 20

Multilevel Inheritance

A sub class will be inheriting a super class and as well as the sub class will also act as the super class to other class.

class GrandParent {
    private int x;

    public void setX(int x) {
        System.out.println("Setting x in GrandParent class...");
        this.x = x;
    }

    public int getX() {
        return this.x;
    }
}

class Parent extends GrandParent { // acquiring property of GrandParent class
    int y;

    public void setY(int y) {
        System.out.println("Setting y in Parent class...");
        this.y = y;
    }

    public int getY() {
        return this.y;
    }
}

class Child extends Parent { // acquiring property of Parent class
    private int z;

    public void setZ(int z) {
        System.out.println("Setting z in Child class...");
        this.z = z;
    }

    public int getZ() {
        return this.z;
    }
}

class MultiLevelInheritance {
    public static void main(String[] arr) {
        Child obj = new Child(); // Contains properties of Child, Parent and GrandParent classes
        obj.setZ(10);
        System.out.println("Age of child is: " + obj.getZ());
        obj.setY(20);
        System.out.println("Age of parent is: " + obj.getY());
        obj.setX(30);
        System.out.println("Age of grandparent is: " + obj.getX());
    }
}

Output :

Setting z in Child class...
Age of child is: 10
Setting y in Parent class...
Age of parent is: 20
Setting x in GrandParent class...
Age of grandparent is: 30

Hierarchical Inheritance

One class serves as a super class for more than one sub classes.

class GrandParent {
    private int x;

    public void setX(int x) {
        System.out.println("Setting x in GrandParent class...");
        this.x = x;
    }

    public int getX() {
        return this.x;
    }
}

class Parent extends GrandParent { // acquiring property of GrandParent class
    int y;

    public void setY(int y) {
        System.out.println("Setting y in Parent class...");
        this.y = y;
    }

    public int getY() {
        return this.y;
    }
}

class Child extends GrandParent { // acquiring property of GrandParent class
    private int z;

    public void setZ(int z) {
        System.out.println("Setting z in Child class...");
        this.z = z;
    }

    public int getZ() {
        return this.z;
    }
}

class HierarchicalInheritance {
    public static void main(String[] arr) {
        Parent obj1 = new Parent(); // Contains properties of Parent and GrandParent classes
        obj1.setY(20);
        System.out.println("Age of parent is: " + obj1.getY());
        obj1.setX(30);
        System.out.println("Age of grandparent is: " + obj1.getX());
        Child obj2 = new Child(); // Contains properties of Child and GrandParent classes
        obj2.setZ(10);
        System.out.println("Age of child is: " + obj2.getZ());
        obj2.setX(30);
        System.out.println("Age of grandparent is: " + obj2.getX());
    }
}

Output :

Setting y in Parent class...
Age of parent is: 20
Setting x in GrandParent class...
Age of grandparent is: 30
Setting z in Child class...
Age of child is: 10
Setting x in GrandParent class...
Age of grandparent is: 30

Multiple Inheritance

One sub class can have more than one super class and inherit features from all parent classes.
In Java, we can achieve multiple inheritance only through interfaces, and not with classes.

Hybrid Inheritance

One class serves as a super class for more than one sub classes and these sub classes can be serve as a super class for one sub class.
In Java, we can achieve hybrid inheritance only through interfaces.

Constructor Inheritance

class GrandParent {
    public GrandParent() {
        System.out.println("This is a constructor in GrandParent class.");
    }
}

class Parent extends GrandParent {
    public Parent() { // Calls the constructor of GrandParent class
        System.out.println("This is a constructor in Parent class.");
    }
}

class Child extends Parent {
    public Child() { // Calls the constructor of Parent class and Child class
        System.out.println("This is a constructor in Child class.");
    }
}

class MultipleInheritance {
    public static void main(String[] arr) {
        new Child(); // Constructor Calling
    }
}

Output :

This is a constructor in GrandParent class.
This is a constructor in Parent class.
This is a constructor in Child class.


Previous

Next

Comments