Inheritance Interview Questions Part I

What is inheritance?
  • Inheritance is one of the oops concepts in java.inheritance is concept of getting properties of one class object to another class object.

  • Inheritance represents the IS-A relationship,also known as parent-child relationship.

Why we need to use Inheritance?
  1. For Code Reusability.

  2. For Method Overriding.

What are the types of inheritance?
  1. Multiple inheritance (java doesn't support multiple inheritance).

  2. Multilevel inheritance.

Learn more about Inheritance and it's types.

What is syntax of inheritance?
What is the output of this program?

Options :

  1. 0

  2. 1

  3. 2

  4. Compilation Error

Answer : c.

Explanation : class A & class B contain display() method, class B inherits class A, when display() method is called by object of class B, display() method of class B is executed rather than that of Class A.

How Inheritance can be implemented in java?

Inheritance can be implemented in JAVA using below two keywords :

  1. extends

  2. implements

  • extends is used for developing inheritance between two classes and two interfaces.

  • implements keyword is used to developed inheritance between interface and class.

What is multilevel inheritance?

Multilevel inheritance getting the properties from one class object to another class object level wise with different priorities.

What is Multiple inheritance? Why Java doesn't support multiple inheritance?
  • The concept of getting the properties from multiple class objects to sub class object with same priorities is known as multiple inheritance.

  • In multiple inheritance there is every chance of multiple properties of multiple objects with the same name available to the sub class object with same priorities leads for the ambiguity also known as diamond problem. One class extending two super classes.

  • Because of multiple inheritance there is chance of the root object getting created more than once.

  • Always the root object i.e object of object class hast to be created only once.

  • Because of above mentioned reasons multiple inheritance would not be supported by java.

  • Thus in java a class cannot extend more than one class simultaneously. At most a class can extend only one class.

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.

Which of these keywords is used to refer to member of base class from a sub class?
  1. upper

  2. super

  3. this

  4. None of the mentioned.

Answer : b.

Explanation : Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super.

How do you implement multiple inheritance in java?

Using interfaces java can support multiple inheritance concept in java. In java we cannot extend more than one classes, but a class can implement more than one interfaces.

Program:

What is the output of this program?

Options :

  1. 2 2

  2. 3 3

  3. 2 3

  4. 3 2

Answer : c.

Explanation : None.

Can a class extend itself?

No, A class can't extend itself.

What happens if super class and sub class having same field name?

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

Does Java support Multiple Inheritance ?

No, Java doesn't support multiple inheritance. Interfaces doesn't facilitate inheritance and hence implementation of multiple interfaces doesn't make multiple inheritance.

Are constructors inherited? Can a subclass call the parent's class constructor? When?
  • You cannot inherit a constructor. That is, you cannot create a instance of a subclass using a constructor of one of it's superclasses.

  • One of the main reasons is because you probably don't want to override the superclasses constructor, which would be possible if they were inherited.

  • By giving the developer the ability to override a superclasses constructor you would erode the encapsulation abilities of the language.

What is the output of this program?

Options :

  1. 2 2

  2. 3 3

  3. Runtime Error

  4. Compilation Error

Answer : d.

Explanation : class contains a private member variable j, this cannot be inherited by subclass B and does not have access to it.

Why java doesn't support multiple Inheritance ?

A and B test() methods are inheriting to C class. So which test() method C class will take? As A & B class test() methods are different , So here we would Facing Ambiguity.

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.

What is the output of this program?

Options :

  1. 1 2

  2. 2 1

  3. Runtime Error

  4. Compilation Error

Answer : a.

Explanation : Keyword super is used to call constructor of class A by constructor of class B. Constructor of a initializes i & j to 1 & 2 respectively.

A class member declared protected becomes member of subclass of which type?

Options :

  • public member

  • private member

  • protected member

  • static member

Answer : b.

Explanation : A class member declared protected becomes private member of subclass.

Difference Between this() and super() ?
  • this is a reference to the current object in which this keyword is used whereas super is a reference used to access members specific to the parent Class.

  • this is primarily used for accessing member variables if local variables have same name, for constructor chaining and for passing itself to some method whereas super is primarily used to initialize base class members within derived class constructor.

What is the output of this program?

Options :

  1. 12

  2. 21

  3. 13

  4. 31

Answer : a.

Explanation : Both class A & B have member with same name that is j, member of class B will be called by default if no specifier is used. I contains 1 & j contains 2, printing 1 2.

What will happen if class implement two interface having common method?

That would not be a problem as both are specifying the contract that implement class has to follow. If class C_JBK implement interface A_JBK & interface B_JBK then Class C_JBK thing I need to implement print() because of interface A_JBK then again Class think I need to implement print() again because of interface B_JBK, it sees that there is already a method called test() implemented so it's satisfied.

What are points to consider in terms of access modifier when we are overriding any method?
  1. Overriding method cannot be more restrictive than the overridden method.

    Reason : In case of polymorphism , at object creation jvm look for actual runtime object. jvm does not look for reference type and while calling methods it look for overridden method.

    If by means subclass were allowed to change the access modifier on the overriding method, then suddenly at runtime—when the JVM invokes the true object's version of the method rather than the reference type's version then it will be problematic.

  2. In case of subclass and superclass define in different package, we can override only those method which have public or protected access.

  3. We cannot override any private method because private methods can not be inherited and if method cannot be inherited then method can not be overridden.

Learn in Detail about Java Programming