Saturday, 30 July 2016

Inheritance Interview Questions in Java

Inheritance Interview Questions in Java

Hello friends I an here with new concept Related to Inheritance in Java

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

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.

Give Example of Inheritance in Java

public class B
{
  void f1(){}  
}
public class A extends B
{
  void f2(){}
}
void main(String[] args)
{
  A a =  new A();
  a.f2();
}

What is the output of this program?

     class A {
     public int i;
     public int j;
     A() {
     i = 1;
     j = 2;
      }
     }
     class B extends A {
     int a;
     B() {
     super();
      }
     }
     class super_use {
     public static void main(String args[])
     {
     B obj = new B();
     System.out.println(obj.i + " " + obj.j)
      }
     } 

Output:

1

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.  Does a class inherit the constructor of its super class?

Ans. No.

Reference: Inheritance Interview Questions in Java

No comments:

Post a Comment