Inheritance in Java with example

Here we are going to discuss Inheritance in Java with a simple example. Inheritance is a Object Oriented Programming concepts, which allows to extend a superclass/parentclass to a subclass. Interface in java is used for class inheritance and interface inheritance. extend and implement keywords are used for inheritance in Java.

Inheritance in Java with example

Here we are going to discuss Inheritance in Java with a simple example. Inheritance is a Object Oriented Programming concepts, which allows to extend a superclass/parentclass to a subclass. Interface in java is used for class inheritance and interface inheritance. extend and implement keywords are used for inheritance in Java.

Inheritance in Java with example

Here we are going to discuss Inheritance in Java with a simple example. Inheritance is a Object Oriented Programming concepts, which allows to extend a superclass/parentclass to a subclass. Interface in java is used for class inheritance and interface inheritance. extend and implement keywords are used for inheritance in Java.

Inheritance is used in java for the reusability of code.

The extends keyword indicates that the features of a superclasses class is inherited by its subclass. Inheritance allows one class to inherit from another class by using extend keyword and every class is a subclass of java.lang.Object.java.

Java supports three types of inheritance, they are:

  • Simple inheritance
  • Multilevel inheritance.
  • Hierarchical inheritance

Note:

1. subclass cannot inherit members that are declared private in superclass. However, these can be accessed indirectly.
2. In other packages Subclass cannot inherit the default members of the superclass but in the same package it can access them by their simple names.
3. subclass do not inherit constructors and initializer blocks as they are not members of a class.
4. subclass can extend only single superclass

Simple inheritance: The simple inheritance is superclass inherited by subclass. In simple inheritance one class extend one class only.

Example of simple inheritance:

class SimpleInherit {
	int ab;
	int cd;

	int get(int pq, int qr) {
		ab = pq;
		cd = qr;
		return(0);
	}

	void Show() {
		System.out.println(ab);
	}
}

public class SimpleInheritText extends SimpleInherit {
	public static void main(String args[]) {
		SimpleInherit text = new SimpleInherit();
		text.get(25,26);
		text.Show();
	}
     }

Output:

Multilevel inheritance: The Multilevel inheritance is inheriting features of more than one class into a single class. Multiple inheritance in Java can be achieved by interface. In this example we have used multilevel inheritance to create one-to-one ladder. "super" keyword is used to inherit features of a superclass into subclass in Multilevel inheritance.

Example of Multilevel inheritance:

class College {
    public void branch(String str) {
        System.out.println("Branch is MCA ");
        

    }
}

class Bipul extends College {
    public void branch(String str) {
        System.out.println("Bipul Branch is BAC:");

    }
}

class Gopal extends College {
    public void branch(String str) {
        System.out.println("Gopal Branch is MBA: ");

    }
}

class Ankit extends College {
    public void branch(String str) {
        System.out.println("Ankit Branch is B.Tech: ");

    }
}

public class Student {
    public static void main(String[] args) {
        Bipul b = new Bipul();
        Gopal c = new Gopal();
        Ankit d = new Ankit();
         b.branch("MCA");
         c.branch("BCA");
         d.branch("MBA");
    }
}

Output:

Hierarchical inheritance: Hierarchical inheritance, a superclass is extended by more than one class in a tree structure.

Syntax of hierarchical inheritance:

class A//A is superclass class
{
body declaration 
}
class B extends A
{
//body declaration 
}
class c extends B
{
'//body declaration 
}
class D
{
public static void main (string args[])
{
// body declaration 
}
}

Download Source Code

Download Source Code