Friday, 31 May 2013

Multiple Inheritance in JAVA

Multiple Inheritance:
Multiple inheritance is where we inherit the properties and behavior of multiple classes to a single class.


Why Java does not support multiple inheritance?

Now we are sure that there is no support for multiple inheritance in java. But why?

JAVA: A simple, object oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high performance, multithreaded, dynamic language.

Look at the beauty of this definition for java. This should be the definition for a modern software language. What is the first characteristic in the language definition? It is simple.

In order to enforce simplicity should be the main reason for omitting multiple inheritance. For instance, we can consider below problem of multiple inheritance.



We have two classes B and C inheriting from A. Assume that B and C are overriding an inherited method and they provide their own implementation. Now D inherits from both B and C doing multiple inheritance. D should inherit that overridden method, which overridden method will be used? Will it be from B or C? Here we have an ambiguity.

Where as using interfaces we could not get the ambiguity problem. See the below.

A class can implement multiple interfaces. In that case the class must implement all the methods declared in all the interfaces implemented. 

Multiple inheritance is not supported in case of classes but it is supported in case of interfaces because there is no ambiguity in as implementation of method is provided by implementation class.

Here is an example:

********************************************************

public interface FourWheeler {

public void getPrice();
        public void getCompany();
}

=======================================

public interface TwoWheeler {

public void getWheels();
        public void getCompany();
}

=======================================

/**
 * Here Class implements the multiple interfaces. Here we have getCompany()
 * method in both the interfaces and we are implementing both the interfcaes.
 * but we did not get any ambiguity because method implementation is 
 * provided by class.So there is no ambiguity in interfaces. So multiple 
 * inheritance is supported by interfaces.
 */
public class Vehicle implements TwoWheelerFourWheeler {

/**
 * @param args
 */
public static void main(String[] args) {
/**
 * Here we are creaintg the instace of an class and assign it to
 * reference of an class.SO we ca naceess the bothe the methods 
   * in 2 interfaces.
 */
     Vehicle vehicle = new Vehicle();
     vehicle.getWheels();
           vehicle.getPrice();
     vehicle.getCompany();
/**
 * Here we are creating instance of class but assign it to reference 
   * of TwoWheeler interfcae. So we can not access the method of 
   * FourWheeler interface method. so comment the line of code.
 */
    TwoWheeler twoWheeler = new Vehicle();
    twoWheeler.getWheels();
    twoWheeler.getCompany();
    // twoWheeler.getPrice();
/**
 * Here we are creating instance of class but assgn it to reference 
   * of FourWheeler interfcae. So we can not access the method of 
   * TwoWheeler interface method. so commentd the line of code.
 */
    FourWheeler fourWheeler = new Vehicle();
    fourWheeler.getPrice();
    twoWheeler.getCompany();
    // fourWheeler.getWheels();
}

@Override
public void getPrice() {
System.out.println("get 4 whellers price");
}

@Override
public void getWheels() {
System.out.println("get 2 wheelers models");
}

@Override
public void getCompany() {
System.out.println("get company of vehicles");
}
  }

********************************************************
So, remember that although Java does not have multiple inheritance, a Java class does have the ability to implement multiple interfaces instead.

No comments:

Post a Comment