Inheritance Interview Question in java

Inheritance Interview Question in java

Please find below the list of Inheritance Interview Question in java:

1) What do you mean by inheritance.?

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.

2) What are the types of inheritance.?

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.)

3) Can a class extend more than one classes or does java support multiple inheritance? If not, why?

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.

 

}

4) How do you implement multiple inheritance in java?

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.

}

 

5) You know that all classes in java are inherited from java.lang.Object class. Are interfaces also inherited from Object class.?

No, only classes in java are inherited from Object class. Interfaces in java are not inherited from Object class. But, classes which implement interfaces are inherited from Object class

6) How do you restrict a member of a class from inheriting to it’s sub classes.?

By declaring that member as a private. Because, private members are not inherited to sub classes.

7) Can a class extend itself.?

No, A class can not extend itself.

8) Are constructors and initializers also inherited to sub classes.?

No, Constructors and initializers(Static initializers and instance initializers) are not inherited to sub classes. But, they are executed while creating object a sub class.

9) What happens if both, super class and sub class, have a field with same name.?

Super class field will be hidden in the sub class. You can access hidden super class field in sub class using super keyword.

10) Are static members inherited to sub classes?

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.

}

}

This is all about Inheritance Interview Question and answers.

Related Posts:

TOP 200+ JAVA Interview Questions And Answers

TOP 50+ DBMS Interview Questions – 2022

Python Interview Question And Answers

HTML Interview Questions And Answers

Mostly Asked PHP Interview Questions And Answers

Angular Interview Questions and Answers

Cloud Computing Interview Questions And Answers