Inheritance in java with examples

Java is Object-Oriented Programming. The core concept of OOPs programming is Encapsulation, Inheritance, Class, Object, Abstraction and Polymorphism.

Inheritance is one of the main core concepts in Java. Inheritance uses to reuse the code of other class. The class which is using another class property is known as a subclass and whose property is using known as the superclass. Inheritance defines the relationship is-a between superclass and its subclass

Other names of the subclass are child class, derived class, and subtype.

And similarly, for superclass are parent class, base class, and supertype.

In principle, as a core concept of Inheritance, Java support only Single Level inheritance. Other types of inheritance can be used in another way (By using implementation of the interface) but not using class concept.

When new class derived from old class, fields and method can be override or hidden or inherited.

For example any fields or method of the superclass, those have private accessibility, will not inherit by the subclass.

Inheritance java example:-

class MySuperClass 
{
public int mscA, mscB;
protected int mscC, mscD;
private int mscE;
static int mscG = 5;
MySuperClass() 
{
System.out.println("No Argument Constructor of MySupperClass");
mscA = 1;
mscB = 2;
mscC = 3;
mscD = 4;
mscE = 30;
}
}
class MySubClass extends MySuperClass 
{
public int mscB;
protected int mscD;
private int mscE;
MySubClass() 
{
System.out.println("No Argument Constructor of MySubClass");
mscA = 11;// inherit from super class
mscB = 12;// mscB of superclass get hide by subclass
mscC = 13;// inherit from super class
mscD = 14; // mscD of superclass get hide by subclass
mscE = 15;// private fields of superclass do not inherit
mscF = 16; // private fields of superclass do not inherit
}
}
public class test 
{
public static void main(String[] args) 
{
MySubClass object = new MySubClass();
object.printvalue();
}
}

 

Output:-

No Argument Constructor of MySupperClass.

No Argument Constructor of MySubClass.

Java Inheritance example Explanation:-

When instance of MySubClass created it give output as:

No Argument Constructor of MySupperClass.

No Argument Constructor of MySubClass.

Question is that why and how MySupperClass constructor called.

Answer is, Compiler replace subclass constructor as:

MySuperClass() 
{
Super();
System.out.println("No Argument Constructor of MySupperClass");
mscA = 1;
mscB = 2;
mscC = 3;
mscD = 4;
mscE = 30;
}

 

So compiler added super() as a first line in subclass constructor, and internally this          super() calls Superclass constructor.

From above example, we can conclude following

  • The private variables/fields of the superclass cannot be inherited.
  • public and private variables/fields of the superclass are inherited by a subclass
  • if subclass uses the same variable name as used in super class then that variable would be override

Overridden Method: If subclass redefines the superclass’s method, subclass’s method called as overridden method.

There is few rule to override the method, these are

  1. Superclass Function that should be overridden in subclass, cannot be static and final.
  2. In superclass, the function can have public, protected and default accessibility, while in subclass it can be same as in super class or widen than as declaring in the super.

For example:-

If is superclass function has protected accessibility than in subclass same function can have protected or public accessibility.

  1. The new method definition must have the same method signature, e., the method name, and the types and the number of parameters, including their order, are the same as in the overridden method.
  2. return type can be the covariant type, i.e. it should be object reference only and in subclass function return type must be only subclass or return type function is a superclass.

Example of Java Inheritance:-

class superclass

{

protected Object fun(int value)

{

Object obj = new Obect();

return obj;

}

}

class subclass extends superclass

{

public String fun(int value)

{

String s = “hello”;

return s;

}

}

Explanation of above example:-

Function “fun” is overriding in subclass please see how

  • In super class accessibility of function “fun” is protected, as par rule number 2 in subclass accessibility should be widened, i.e. it can be either protected or public. In subclass ‘fun’ has public accessibility so rule 2 satisfy.
  • If the return type is any class as par rule 4, the return type of function ‘fun’ in a subclass can be any subclass of Object class, and in our example, subclass class ‘fun’ return type is String which is a subclass of Object class. So rule 4 is to
  • Signature of function ‘fun’ is same so rule 3 is also satisfy.
5/5 - (1 vote)

royal52
royal52

I’m a DevSecOps Software engineer by profession and a SEO hobbyist. I’m passionate about everything Software, including game development.

We will be happy to hear your thoughts

Leave a Reply

HellGeeks
Logo