Inheritance in java with example programs: Java Inheritance is a process where one class obtains the properties (methods and fields) of another class. This tutorial on Inheritance in Java clarifies all your queries like What is Inheritance Exactly, their Types, Uses of Java Inheritance, etc. all with a neat explanation. By using the available links, you will easily jump into the respective concepts of Inheritance in Java OOPs with Examples provided further.
- Inheritance in Java OOPs
- is-a relationship
- Why use Inheritance?
- Java Inheritance (Subclass and Superclass)
- Syntax: Inheritance in Java
- extend Keyword
- Java Inheritance Example
- Types of Inheritance in Java
- 1. Single Inheritance
- 2. Multiple Inheritance
- 3. Multilevel Inheritance
- 4. Hierarchical Inheritance
- 5. Hybrid Inheritance
- Why multiple inheritance is not supported in java?
Inheritance in Java OOPs
Example of inheritance: Inheritance is one of the key features of object-oriented programming (OOPs). Inheritance is a process of inheriting the properties and behavior of the existing class into a new class. When we inherit the class from an existing class, we can reuse the methods and fields of the parent class. Inheritance can be defined as the Is-A relationship, which is also known as the parent-child relationship.
Also Check:
is-a relationship
Java Inheritance is an is-a relationship where we use to say the object is a type of that object. For instance,
- Dog is an Animal
- Car is a Vehicle
- Surgeon is a Doctor
- Orange is a Fruit
Here, Dog can inherit from Animals, Car can inherit from Vehicle, and so on.
Why use Inheritance?
- The main purpose of inheritance is code reusability. We can reuse the code when we inherit the properties and behavior of the existing class into a new class.
- The runtime polymorphism (method overriding) can be achieved by inheritance.
Java Inheritance (Subclass and Superclass)
- Class: Class is used to describe the properties and behavior of an object. It is a blueprint of an object.
- Super Class: Super Class is also known as a parent class or a base class. The superclass is the class whose features are inherited from a subclass.
- Sub Class: Sub Class is also known as a child class or a derived class or an extended class. Sub Class is the class that inherits the other class.
Syntax: Inheritance in Java
class SuperClass{ //methods and fields } class SubClass extends SuperClass{ //methods and fields }
extend Keyword
The extends is a keyword that is used to inherit the properties and behavior of a class. Below is the syntax of extends keyword.
class Super { ..... ..... } class Sub extends Super { ..... ..... }
Java Inheritance Example
class Person{ private String name; private int age; public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setAge(int age){ this.age = age; } public int getAge(){ return age; } } class Student extends Person{ private int rollno; public void setRollno(int rollno){ this.rollno = rollno; } public int getRollno(){ return rollno; } } class InheritanceExample{ public static void main(String args[]){ Student obj = new Student(); obj.setRollno(101); obj.setName("Prashant"); obj.setAge(21); System.out.println("Student rollno is: "+obj.getRollno()); System.out.println("Student name is: "+obj.getName()); System.out.println("Student age is: "+obj.getAge()); } }
Output:
Types of Inheritance in Java
The following list of the five names is the type of inheritance supported by Java. They are as follows,
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Multiple Inheritance, and
- Hybrid Inheritance
These five types of java inheritance are discussed below with a flowchart and example programs.
Note: Multiple and Hybrid Inheritance in java can be supported through interface only.
1. Single Inheritance
In single inheritance, one class can extend the functionality of another class. In single inheritance only one parent and one child class is present.
Single Inheritance Example:
class SuperClass{ //methods and fields } class SubClass extends SuperClass{ //methods and fields } Example of Single Inheritance: class Birds{ public void birdsEat(){ System.out.println("Birds are eating"); } } class Crow extends Birds{ public void crowEat(){ System.out.println("Crow is eating"); } } class Person{ public static void main(String args[]){ Crow obj = new Crow(); obj.birdsEat(); obj.crowEat(); } }
Output:
2. Multiple Inheritance
In multiple inheritances, there is more than one parent class and one child class is present. The child class inherits from more than one parent class. Java doesn’t support multiple inheritances. It can be supported through the interface only.
Multiple Inheritance Example:
// Java program to illustrate the // concept of Multiple inheritance import java.io.*; import java.lang.*; import java.util.*; interface one { public void print_geek(); } interface two { public void print_for(); } interface three extends one, two { public void print_geek(); } class child implements three { @Override public void print_geek() { System.out.println("Geeks"); } public void print_for() { System.out.println("for"); } } // Drived class public class Main { public static void main(String[] args) { child c = new child(); c.print_geek(); c.print_for(); c.print_geek(); } }
Output:
Geeks for Geeks
3. Multilevel Inheritance
In multilevel inheritance, there is more than one level. If one class can inherit from a derived class and the derived class becomes the base class of the new class then it is called multilevel inheritance.
Multilevel Inheritance Example:
class A { // methods and fields; } class B extends A { // methods and fields } class C extends B { // methods and fields } Example of Multilevel Inheritance: class Birds{ public void birdsEat(){ System.out.println("Birds are eating"); } } class Crow extends Birds{ public void crowEat(){ System.out.println("Crow is eating"); } } class BabyCrow extends Crow{ public void babyCrowEat(){ System.out.println("Baby Crow is eating"); } } class MultipleInheritance{ public static void main(String args[]){ BabyCrow obj = new BabyCrow(); obj.birdsEat(); obj.crowEat(); obj.babyCrowEat(); } }
Output:
4. Hierarchical Inheritance
In Hierarchical inheritance, from a single parent class, we are inheriting multiple child classes. In the below diagram, class A inherits class B, class C, and class D.
Hierarchical Inheritance Example:
class A { // methods and fields; } class B extends A { // methods and fields } class C extends A { // methods and fields } class D extends A { // methods and fields } Example of Hierarchical Inheritance: class Birds{ public void birdsEat(){ System.out.println("Birds are eating"); } } class Crow extends Birds{ public void crowEat(){ System.out.println("Crow is eating"); } } class Sparrow extends Birds{ public void sparrowEat(){ System.out.println("Sparrow is eating"); } } class Person{ public static void main(String args[]){ Sparrow obj = new Sparrow(); obj.birdsEat(); obj.sparrowEat(); obj.crowEat(); //compile-time error } }
Output:
5. Hybrid Inheritance
If we are using multiple types of inheritance simultaneously such types of inheritance are called Hybrid Inheritance. But java does not support Hybrid Inheritance. Hybrid Inheritance can be supported through interface only.
Hybrid Inheritance Example:
class C { public void disp() { System.out.println("C"); } } class A extends C { public void disp() { System.out.println("A"); } } class B extends C { public void disp() { System.out.println("B"); } } class D extends A { public void disp() { System.out.println("D"); } public static void main(String args[]){ D obj = new D(); obj.disp(); } }
Output:
D
Why multiple inheritance is not supported in java?
Java doesn’t support multiple inheritances due to the ambiguity problem.
Consider a scenario where X, Y, and Z are the three classes. The Z class inherits both the class i.e. class X and class Y. If class X and class Y have the same method and we call it from child class object ( class Z object), there will be ambiguity to call the method of class X or a class Y. Therefore compiler show compiles time error if we inherit 2 classes.
class X { public void show(){ System.out.println("We are in X class"); } } class Y extends X{ public void show(){ System.out.println("We are in Y class"); } } class Person extends X,Y{ public static void main(String args[]){ Person obj = new Person(); obj.show(); //Now which show() method would be invoked? } }