Easy Tutorial
For Competitive Exams

Java Programming Inheritance

What is Inheritance?

Inheritance is the process of deriving new class from existing classes.

Why Inheritance?

Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality.

The concept of inheritance greatly enhances the ability to reuse code as well as making design a much simpler and easier process.

A subclass inherits all non-private variables and methods from its super class and also has its own variables and methods.

One can inherit the class using keyword extends.

Syntax

class subclass-name extends super class-name
{
            // body of class.
}

When Inheritance?

When a “Is-A” relationship exists between two classes we use Inheritance.

The parent class is termed super class and the inherited class is the sub class.

The keyword extends is used by the sub class to inherit the features of super class.

Inheritance is important since it leads to reusability of code.

What is IS-A relationship?

"IS-A" relationship describes inheritance relationship between objects. If you can talk something with word "is". They can be described with keyword extends in coding. "is-a" relationship is also called classification.

Example

public class Animal{
}

public class Mammal extends Animal{
}

public class Reptile extends Animal{
}

Now, based on the above example, In Object Oriented terms, the following are true:

  • Animal is the superclass of Mammal class.
  • Animal is the superclass of Reptile class.
  • Mammal and Reptile are subclasses of Animal class.

Now, if we consider the IS-A relationship, we can say:

  • Mammal IS-A Animal
  • Reptile IS-A Animal

Example

class Employee
{
    public void p1()
    {
        System.out.println("Employee method");
    }
}
public class Manager extends Employee {
    public void c1()
    {
        System.out.println("Manager method");
    }
    public static void main(String[] args)
    {
       Manager cobj = new Manager();
        cobj.c1();   //method of Child class
        cobj.p1();   //method of Parent class 
    }
}
Try it Yourself

What is Has-A relationship?

HAS-A relationships are based on usage, rather than inheritance. In other words class A has-a relationship with class B, if code in class A has a reference to an instance of class B.

Example

class Author{
 String authorName;
 int age;
 String place;
 Author(String name,int age,String place){
  this.authorName=name;
  this.age=age;
  this.place=place;
 }
 public String getAuthorName(){
  return authorName;
 }
 public int getAge(){
  return age;
 }
 public String getPlace(){
  return place;
 }
}

class Book{
 String name;
 int price;
 Author auth;
 Book(String n,int p,Author at){
  this.name=n;
  this.price=p;
  this.auth=at;
 }
 public void showDetail(){
  System.out.println("Book is"+name);
  System.out.println("price "+price);
  System.out.println("Author is "+auth.getAuthorName());
 }
}

class Test{
 public static void main(String args[]){
  Author ath=new Author("Me",22,"India");
  Book b=new Book("Java",550,ath);
  b.showDetail();
 }
}
Try it Yourself

What is not possible using java class Inheritance?

Private members of the super class are not inherited by the subclass and can only be indirectly accessed.

Members that have default accessibility in the super class are also not inherited by subclasses in other packages, as these members are only accessible by their simple names in subclasses within the same package as the super class.

Since constructors and initializer blocks are not members of a class, they are not inherited by a subclass.

A subclass can extend only one super class

Java does not support multiple inheritance.

Types of Inheritance:

  • Single Inheritance
  • Hierarchical Inheritance
  • Multilevel Inheritance
  • Multiple Inheritance (Through Interface)
  • Hybrid Inheritance (Through Interface)

What is Single Inheritance in Java?

Single Inheritance is the simple inheritance of all, When a class extends Only one class then we call it as Single inheritance.

Example

public class ClassA {
    public void dispA(){
        System.out.println("disp() method of ClassA");
    }
}
public class ClassB extends ClassA {
    public void dispB(){
        System.out.println("disp() method of ClassB");
    }
    public static void main(String args[]){
        ClassB b = new ClassB();
        b.dispA();
        b.dispB();
    }
}
Try it Yourself

What is Hierarchical Inheritance in Java?

In Hierarchical inheritance one parent class will be inherited by many sub classes.

Example

public class ClassA {
    public void dispA(){
        System.out.println("disp() method of ClassA");
    }
}
public class ClassB extends ClassA {
    public void dispB(){
        System.out.println("disp() method of ClassB");
    }
}
public class ClassC extends ClassA{
    public void dispC(){
        System.out.println("disp() method of ClassC");
    }
}
public class ClassD extends ClassA{
    public void dispD(){
        System.out.println("disp() method of ClassD");
    }
}
public class HierarchicalInheritanceTest {
    public static void main(String args[]){
         ClassB b = new ClassB();
         b.dispB();
         b.dispA();
         ClassC c = new ClassC();
         c.dispC();
         c.dispA();
         ClassD d = new ClassD();
         d.dispD();
         d.dispA();
    }
}
Try it Yourself

What is Multilevel Inheritance in Java?

In Multilevel Inheritance a derived class will be inheriting a parent class and as well as the derived class act as the parent class to other class.

Example

public class ClassA {
    public void dispA(){
        System.out.println("disp() method of ClassA");
    }
}
public class ClassB extends ClassA {
    public void dispB(){
        System.out.println("disp() method of ClassB");
    }
}
public class ClassC extends ClassB{
    public void dispC(){
        System.out.println("disp() method of ClassC");
    }
    public static void main(String args[]){
         ClassC c = new ClassC();
         c.dispA();
         c.dispB();
         c.dispC();
    }
}
Try it Yourself

What is Multiple Inheritance in Java?

Multiple Inheritance is nothing but one class extending more than one class.

Multiple Inheritance is basically not supported by many Object Oriented Programming languages such as Java, Small Talk, C# etc.. (C++ Supports Multiple Inheritance).

As the Child class has to manage the dependency of more than one Parent class.

But you can achieve multiple inheritance in Java using Interfaces.

What is Hybrid Inheritance in Java?

Hybrid Inheritance is the combination of both Single and Multiple Inheritance.

Again Hybrid inheritance is also not directly supported in Java only through interface we can achieve this.

Solve this!!

11801.Which of these is correct way of inheriting class A by class B?
class B + class A {}
class B inherits class A {}
class B extends A {}
class B extends class A
11802.What will be the output of the following program?
class A{
    int i = 10;
}
 
class B extends A{
    int i = 20;
}
 
class Demo{
    public static void main(String[] args){
        B a = new B();
       System.out.println(a.i);
    }
}
10
20
0
Error
11803.What will be the output of the following program?
class A{
    {
        System.out.println(1);
    }
}
 
class B extends A{
    {
        System.out.println(2);
    }
}
 
class C extends B{
    {
        System.out.println(3);
    }
}
 
 class Demo{
    public static void main(String[] args){
        C c = new C();
    }
}
1 2 3
1
2
3
Share with Friends