Java - Interview Questions and Answers on Inheritance

Q1.  Does Java support Multiple Inheritance ?

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

Q2.  Why java doesn't support multiple Inheritence ?

Ans. class A {
      void test() {
          System.out.println("test() method");
      }
}

class B {
         void test() {
            System.out.println("test() method");
         }
}

Suppose if Java allows multiple inheritance like this,

class C extends A, B {
}

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.


Q3.  Are constructors inherited? Can a subclass call the parent's class constructor? When?

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

Q4.  Can we reduce the visibility of the inherited or overridden method ?

Ans. No.

Q5.  Which of the following is tightly bound ? Inheritance or Composition ?

Ans. Inheritance.

Q6.  Difference Between this() and super() ?

Ans. 1.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. 

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

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

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

Q8.  Does a class inherit the constructor of its super class?

Ans. No.