Inheritance Interview Questions - Part2

Inheritance is one of the key features of object oriented programming. Through inheritance, a class (Sub Class) can inherit properties of another class (Super Class). Sub class can have it’s own properties along with the inherited properties from it’s super class.
There are 5 types of inheritance.

1). Single Inheritance :
One class is extended by only one class.

2). Multilevel Inheritance :
One class is extended by a class and that class in turn is extended by another class thus forming a chain of inheritance.

3). Hierarchical Inheritance :
One class is extended by many classes.

4).Hybrid Inheritance :
It is a combination of above types of inheritance.

5). Multiple Inheritance :
One class extends more than one classes. (Java does not support multiple inheritance.)
co-variant return type states that return type of overriding method can be subtype of the return type declared in method of superclass. it has been introduced since jdk 1.5
1)The overriding methods can throw any runtime Exception , here in the case of runtime exception overriding method (subclass method) should not worry about exception being thrown by superclass method.

2)If superclass method does not throw any exception then while overriding, the subclass method can not throw any new checked exception but it can throw any runtime exception

3) Different exceptions in java follow some hierarchy tree(inheritance). In this case , if superclass method throws any checked exception , then while overriding the method in subclass we can not throw any new checked exception or any checked exception which are higher in hierarchy than the exception thrown in superclass method
No, a class in java can not extend more than one classes or java does not support multiple inheritance. To avoid ambiguity, complexity and confusion, java does not supports multiple inheritance. For example, If Class C extends Class A and Class B which have a method with same name, then Class C will have two methods with same name. This causes ambiguity and confusion for which method to use. To avoid this, java does not supports multiple inheritance.

class A
{
   void methodOne()
{
    System.out.println("From methodOfClassA");
   }
}
class B
  {
   void methodOne()
   {
    System.out.println("From methodOfClassB");
  }
}

class C extends A,B (If it is supported)
{
   //two same methods will be inherited to Class C.
   //This causes ambiguity and confusion.
No, A class can not extend itself.
No, Constructors and initializers(Static initializers and instance initializers) are not inherited to sub classes. But, they are executed while instantiating a sub class.
Through interfaces, we can implement multiple inheritance in java. As classes in java can not extend more than one classes, but a class can implement more than one interfaces.

interface A
{

}

interface B
{

}

class C implements A, B
{
//Class implementing two interfaces.
} 
Super class field will be hidden in the sub class. You can access hidden super class field in sub class using super keyword.
Yes, Static members are also inherited to sub classes.
                            
class A
{
static int i = 10;

static void method()
{
System.out.println("Static Method");
}
}

class B extends A
{
}

public class StaticInitializers
{
public static void main(String[] args)
{
B.method(); //Calling inherited static method

System.out.println(B.i); //printing inherited static field.
}
}