Join Regular Classroom : Visit ClassroomTech

Java Coding For Interview

Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Using inheritance, you can create a general class that defines traits common to a set of related items. This class can then be inherited by other, more specific classes, each adding those things that are unique to it. In the terminology of Java, a class that is inherited is called a Super Class/ Base Class / Parent Class. The class that does the inheriting is called a Subclass/ Child class / Derived Class. Therefore, a subclass is a specialized version of a superclass.

It inherits all of the instance variables and methods defined by the superclass and adds its own, unique elements.

Inheritance in JAVA

Java does not support the following inheritance.

Single Inheritance

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


class Parent {
    void Pdisplay(){
        System.out.println("Parent");
    }
}

//https://codewindow.in 

/*
Parent Class has one method named Pdisplay().
*/

class Child extends Parent{
    void Cdisplay(){
        System.out.println("Child");
    }
}
/*
Child Class has one method named Cdisplay().
And one inherited methods Pdisplay from parent Class.
Child class object has access on Pdisplay and Cdisplay.
*/
class CodeWindow
{
	public static void main (String[] args) 
	{
		Parent p=new Parent();
		p.Pdisplay();
		
		Child c=new Child();
		c.Pdisplay();
		c.Cdisplay();
	}
}

Output:

Parent
Parent
Child

Multilevel Inheritance

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

class GrandParent{
    void Gdisplay(){
        System.out.println("Grand parent");
    }
}
/*
GrandParent Class has one method named Gdisplay().
GrandParent object has access only on Gdisplay.
*/

class Parent extends GrandParent{
    void Pdisplay(){
        System.out.println("Parent");
    }
}


/*
Parent Class has one method named Pdisplay().
And one inherited method Gdisplay from GrandParent Class.
Parent object has access on Gdisplay along with Pdisplay.
*/

class Child extends Parent{
    void Cdisplay(){
        System.out.println("Child");
    }
}
/*
Child Class has one method named Cdisplay().
And two inherited methods Gdisplay from GrandParent Class and Pdisplay from parent Class.
Child class object has access on Gdisplay along with Pdisplay and Cdisplay.

*/
class CodeWindow
{
	public static void main (String[] args) 
	{
		GrandParent gp=new GrandParent();
		gp.Gdisplay();
		
		Parent p=new Parent();
		p.Gdisplay();
		p.Pdisplay();
		
		Child c=new Child();
		c.Gdisplay();
		c.Pdisplay();
		c.Cdisplay();
	}
}

Output

Grand parent
Grand parent
Parent
Grand parent
Parent
Child

Which methods are allowed to inherit?
1. The methods which are public/protected/default method are allowed to extend.


Are Data members allowed to inherit?
Yes. (Those are not private data members.)

Hierarchical Inheritance

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

class Parent{
    void Pdisplay(){
        System.out.println("Parent");
    }
}
/*
Parent Class has one method named Pdisplay().
Parent object can access only on Pdisplay.
*/


class Child1 extends Parent{
    void C1display(){
        System.out.println("Child 1");
    }
}
/*
  Child1 Class extends class Parent.
  Child1 has already one method.
  As Child1 extends Parent class, So it will get Pdisplay() from Parent Class.
  Now Child1 has:
  1. C1display()
  2. Pdisplay()
*/
class Child2 extends Parent{
    void C2display(){
        System.out.println("Child 2");
    }
}
/*
  Child2 Class extends class Parent.
  Child2 has already one method.
  As Child2 extends Parent class, So it will get Pdisplay() from Parent Class.
  Now Child2 has:
  1. C2display()
  2. Pdisplay()
*/
class Child3 extends Parent{
    void C3display(){
        System.out.println("Child 3");
    }
}
/*
  Child3 Class extends class Parent.
  Child3 has already one method.
  As Child3 extends Parent class, So it will get Pdisplay() from Parent Class.
  Now Child3 has:
  1. C3display()
  2. Pdisplay()
*/
class CodeWindow
{
	public static void main (String[] args) 
	{
		
		Parent p=new Parent();
		p.Pdisplay();
		
		Child1 c1=new Child1();
		c1.Pdisplay();
		c1.C1display();
		
		Child2 c2=new Child2();
		c2.Pdisplay();
		c2.C2display();
		
		Child3 c3=new Child3();
		c3.Pdisplay();
		c3.C3display();
	}
}

Output

Parent
Parent
Child 1
Parent
Child 2
Parent
Child 3

Order of Constructor Calling

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

class GrandParent{
    GrandParent()
    {
        System.out.println("Grand Parent class Constructor");
    }
}

class Parent extends GrandParent{
    Parent()
    {
        System.out.println("Parent class Constructor");
    }
}

class Child extends Parent{
    Child()
    {
        System.out.println("Child class Constructor");
    }
}


//https://codewindow.in

class CodeWindow
{
	public static void main (String[] args) 
	{
		
		Child c=new Child();
		
		
	}
}

Explanation:
If we create an object of any Child Class then, the Constructor will be called from the topmost hierarchy to the lower hierarchy.


Output:

Grand Parent class Constructor
Parent class Constructor
Child class Constructor

Method Overriding

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

class GrandParent{
    void display()
    {
        System.out.println("GrandParent");
    }
}

class Parent extends GrandParent{
    void display()
    {
        System.out.println("Parent");
    }
}

class Child extends Parent{
    void display()
    {
        System.out.println("Child");
    }
}




class CodeWindow
{
	public static void main (String[] args) 
	{
		
		GrandParent gp=new GrandParent();
		gp.display();
		
		Parent p=new Parent();
		p.display();
		p.display();
		
		Child c=new Child();
		c.display();
		c.display();
		c.display();
		
		
	}
}

Explanation:
Here the inheritance among GrandParent, Parent, Child is Multi-Level inheritance.

GrandParent has a method named display();
Parent has a method named display();
And also
Child has a method named display();


So, the Parent class has one display() method on its own and another display() method that
is inherited from GrandParent class.

Then Same name two methods with similar signatures will result in Method Overriding.

Priority:
Own method.

 

Output:
GrandParent
Parent
Parent
Child
Child
Child

Super in Inheritance

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

class GrandParent{
    void display()
    {
        System.out.println("GrandParent");
    }
}

class Parent extends GrandParent{
    void display()
    {
        super.display();
        System.out.println("Parent");
    }
}

class Child extends Parent{
    void display()
    {
        super.display();
        System.out.println("Child");
    }
}

class CodeWindow
{
	public static void main (String[] args) 
	{
		
		GrandParent gp=new GrandParent();
		gp.display();
		
		Parent p=new Parent();
		p.display();
		
		Child c=new Child();
		c.display();
		
		
	}
}

The function of super:
super is used to call the immediate Parent Class data member/method.

Output:
GrandParent
GrandParent
Parent
GrandParent
Parent
Child

Super without Inheritance

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

class GrandParent{
    void display1()
    {
        System.out.println("GrandParent");
    }
}

class Parent extends GrandParent{
    void display2()
    {
        super.display1();
        System.out.println("Parent");
    }
}

class Child extends Parent{
    void display3()
    {
        super.display2();
        System.out.println("Child");
    }
}




class CodeWindow
{
	public static void main (String[] args) 
	{
		
		GrandParent gp=new GrandParent();
		gp.display1();
		
		Parent p=new Parent();
		p.display2();
		
		Child c=new Child();
		c.display3();
		
		
	}
}

Explanation:
Without Overriding super can be used.

The function of super:
super is used to call the immediate Parent Class data member/method.

Output:
GrandParent
GrandParent
Parent
GrandParent
Parent
Child

Why is multiple inheritance not supported in java?

1. To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
2. Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from a child class object, there will be ambiguity to call the method of A or B class.
3. Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes. So whether you have the same method or different, there will be a compile time error.

We can implement Multiple Inheritance using Interface.

Static Member Function can not hold non-static data member

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

class Demo{
    //static data member 
   static int a; 
   int b=10;
    
    //static member function / static method
    static void display(){
        a+=1;
        System.out.println(a);
       //ERROR Line no 15
        System.out.println(b);
        /*
        Error 
        non-static variable b cannot be referenced from a static context
        
        static method can not contain any non-static data member / method.
        */ 
    }
}

/*
  Features:
  The value of static data member is 0.
  static data member can be accessed without creating any objects of the class. 
  static method can be accessed without creating any objects of the class.
  
  We can create object of the class. 
  And object of the class can access static method.

*/

class CodeWindow
{
	public static void main (String[] args) 
	{
		Demo.display();  // Accessing static method without Object
		Demo.display();  // Accessing static method without Object
		Demo.display();  // Accessing static method without Object
		
		Demo d = new Demo();
		d.display();     // Accessing static method creating Object
		
		System.out.println(Demo.a); // Accessing static data member without creating Object
		System.out.println(d.a);  // Accessing static data member creating Object
	}
}

Non-static variable b cannot be referenced from a static context.
Static method can not contain any non-static data member/ method.

Features:
The value of static data member is 0.
static data member can be accessed without creating any objects of the class.
static method can be accessed without creating any objects of the class.

We can create object of the class.
And object of the class can access static method.

Another Example

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

class Demo{
    //static data member 
   static int a; 
   //Non-Static data member
   int b=10;
    
    //static member function / static method
    static void display(){
        a+=1;
        System.out.println(a);
         
    }
    //Non-Static Method
    void display1(){
        a+=1; //Static Data member
        System.out.println(a);
        System.out.println(b); //Non-Static data member.
        /*
        Non-Static method can contain Static data member as well as Non-Static data member.
        */
    }
}

/*
  Features:
  The value of static data member is 0.
  static data member can be accessed without creating any objects of the class. 
  static method can be accessed without creating any objects of the class.
  
  We can create object of the class. 
  And object of the class can access static method.

*/

class CodeWindow
{
	public static void main (String[] args) 
	{
		Demo.display();  // Accessing static method without Object
		Demo.display();  // Accessing static method without Object
		Demo.display();  // Accessing static method without Object
		
		Demo d = new Demo();
		d.display();     // Accessing static method creating Object
		d.display1();
		System.out.println(Demo.a); // Accessing static data member without creating Object
		System.out.println(d.a);  // Accessing static data member creating Object
	}
}
Recent Posts