Tuesday, 18 August 2015

Why Java doesn't support multiple inheritance

1)

The first reason is ambiguity around Diamond problem, consider a class A has foo() method and then B and C derived from A and has there own foo() implementation and now class D derive from B and C using multiple inheritance and if we refer just foo() compiler will not be able to decide which foo() it should invoke.

This is also called Diamond problem because structure on this inheritance scenario is similar to 4 edge diamond, see below

A foo()
/ \
/ \
foo() B C foo()
\ /
\ /
D
foo()

In my opinion even if we remove the top head of diamond class A and allow multiple inheritances we will see this problem of ambiguity.

Some times if you give this reason to interviewer he asks if C++ can support multiple inheritance than why not Java. 

hmmmmm in that case I would try to explain him the second reason which I have given below that its not because of technical difficulty but more to maintainable and clearer design was driving factor though this can only be confirmed by any of java designer and we can just speculate.

2)

Second and more convincing reason to me is that multiple inheritances does complicate the design and creates problem during casting, constructor chaining etc and given that there are not many scenario on which you need multiple inheritance its wise decision to omit it for the sake of simplicity.

Also java avoids this ambiguity by supporting single inheritance with interfaces. Since interface only have method declaration and doesn't provide any implementation there will only be just one implementation of specific method hence there would not be any ambiguity.

Tuesday, 4 August 2015

What is the output of following program?

  1. package com.javastepbystep;

  2. class A
  3. {
  4.  
  5.      static int method1(int i)
  6.     {
  7.          return method2(i *= 11);
  8.      }
  9.  
  10.       static int method2(int i)
  11.      {
  12.           return method3(i /= 11);
  13.       }
  14.  
  15.        static int method3(int i)
  16.       {
  17.            return method4(i -= 11);
  18.       }
  19.  
  20.       static int method4(int i)
  21.      {
  22.          return i += 11;
  23.      }
         
  24.      public static void main(String [] args)
  25.     {
  26.           System.out.println(method1(11));
  27.      }
  28.  
  29. }

Ans: 11

Sunday, 2 August 2015

What is the output of following program?

 public class B{
    
      B b= new B();
    
     public int show(){
          return (true ? null : 0);
     }
    
     public static void main(String[] args)  {
    
            B b= new B();
            b.show();
        }
    
    }

Output:
  1. Exception in thread "main" java.lang.StackOverflowError
  2. at com.instanceofjava.B.<init>(B.java:3)
  3. at com.instanceofjava.B.<init>(B.java:5)
  4. at com.instanceofjava.B.<init>(B.java:5) 

Explanation:

  • Whenever we create the object of any class constructor will be called first and memory allocated for all non-static variables
  • Here  B b= new B(); the variable is an object and assigned to a new object of the same class
  • B b= new B(); statement leads to recursive execution of constructor will create infinite objects so at the run time an exception will be raised
  • Exception in thread "main" java.lang.StackOverflowError
  • The common cause for a stack overflow exception is a bad recursive call. Typically this is caused when your recursive functions don't have the correct termination condition