Inheritance

Aditi Dosi
4 min readAug 29, 2022

--

In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object or class, retaining similar implementation. Also defined as deriving new classes from existing ones such as super class or base class and then forming them into a hierarchy of classes.
  • Super Class: The class whose features are inherited is known as superclass(or a base class or a parent class).
  • Sub Class: The class that inherits the other class is known as a subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.
  • Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.

How to use inheritance in Java

The keyword used for inheritance is extends.

Syntax :

class derived-class extends base-class  
{
//methods and fields
}

Types of Inheritance in Java

Below are the different types of inheritance which are supported by Java.

  1. Single Inheritance: In single inheritance, subclasses inherit the features of one superclass. In the image below, class A serves as a base class for the derived class B.

import java.io.*;
import java.lang.*;
import java.util.*;

class one {
public void print_win()
{
System.out.println(“Win”);
}
}

class two extends one {
public void print_for() { System.out.println(“for”); }
}
// Driver class
public class Main {
public static void main(String[] args)
{
two g = new two();
g.print_win();
g.print_for();
g.print_win();
}
}

2. Multilevel Inheritance: In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class. In the below image, class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. In Java, a class cannot directly access the grandparent’s members.

// Java program to illustrate the
// concept of Multilevel inheritance
import java.io.*;
import java.lang.*;
import java.util.*;

class one {
public void print_geek()
{
System.out.println(“Geeks”);
}
}

class two extends one {
public void print_for() { System.out.println(“for”); }
}

class three extends two {
public void print_geek()
{
System.out.println(“Geeks”);
}
}

// Drived class
public class Main {
public static void main(String[] args)
{
three g = new three();
g.print_geek();
g.print_for();
g.print_geek();
}
}

3. Hierarchical Inheritance: In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass. In the below image, class A serves as a base class for the derived class B, C and D.

// Java program to illustrate the
// concept of Hierarchical inheritance

class A {
public void print_A() { System.out.println(“Class A”); }
}

class B extends A {
public void print_B() { System.out.println(“Class B”); }
}

class C extends A {
public void print_C() { System.out.println(“Class C”); }
}

class D extends A {
public void print_D() { System.out.println(“Class D”); }
}

// Driver Class
public class Test {
public static void main(String[] args)
{
B obj_B = new B();
obj_B.print_A();
obj_B.print_B();

C obj_C = new C();
obj_C.print_A();
obj_C.print_C();

D obj_D = new D();
obj_D.print_A();
obj_D.print_D();
}
}

4. Multiple Inheritance (Through Interfaces): In Multiple inheritances, one class can have more than one superclass and inherit features from all parent classes. Please note that Java does not support multiple inheritances with classes. In java, we can achieve multiple inheritances only through Interfaces. In the image below, Class C is derived from interface A and B.

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

5. Hybrid Inheritance(Through Interfaces): It is a mix of two or more of the above types of inheritance. Since java doesn’t support multiple inheritances with classes, hybrid inheritance is also not possible with classes. In java, we can achieve hybrid inheritance only through Interfaces.

Java IS-A type of Relationship.

IS-A is a way of saying: This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance.

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

  1. SolarSystem the superclass of Earth class.
  2. SolarSystem the superclass of Mars class.
  3. Earth and Mars are subclasses of SolarSystem class.
  4. Moon is the subclass of both Earth and SolarSystem classes.
  • Java

class SolarSystem {

}

class Earth extends SolarSystem {

}

class Mars extends SolarSystem {

}

public class Moon extends Earth {

public static void main(String args[])

{

SolarSystem s = new SolarSystem();

Earth e = new Earth();

Mars m = new Mars();

System.out.println(s instanceof SolarSystem);

System.out.println(e instanceof Earth);

System.out.println(m instanceof SolarSystem);

}

}

Output

true
true
true

--

--