Tuesday 13 March 2012

Why Java does not support multiple Inheritance?

One can always think of extending multiple classes to inherit the multiple features in the form of methods.Like following


class Car extends Accelerate,Color{ //interesting code}

but the above declaration is not legal in terms if Java.A class cannot extend more than one class.This exactly implies that only one parent class per class.But a class can have multiple ancestors such that if class A extends class B and class B extends class C then class A has two ancestors class B and C .A class can have multiple ancestors up the inheritance tree but it is not possible by direct declaration of extending classes.

In C++ ,One can extend multiple classes simultaneously that is the declaration we have done for class Car is correct in case of C++.Capability of extending multiple classes is called as "Multiple Inheritance".Java creators thought a lot about allowing the multiple inheritance,but they were messed up after using the multiple inheritance.Like in this case ,if a class extended two classes and if both classes has method(s) in common then how the methods will be inherited and how we will come to know that which method is called of which class.That is why they excluded the multiple inheritance in Java.There is a famous problem which is faced during the multiple inheritance application called as "Deadly Diamond of Death".This is named so because the shape of class diagram which is formed after multiple inheritance implementation.The diamond is formed when classes B and C extend A ,and both B and C inherit methods from A.If class D extends both B and C,and both B and C have overridden the methods in class A,class D in theory has inherited the two different implementations of same method.
:-


There is an indirect way by which you can implement the multiple inheritance in Java and this is through extending a class and implementing an interface just like following:-

class Car extends Accelerate implements Movable{
// implement all Movable methods
}

In this way you can get methods from both class and interface and you can override them according to your use.Remember all methods of interface needs to be overridden in implementing class

Run Postman API remote or code or command line

POSTMAN is used to run the APIs generally. What if we want to create a collection in postman and run it from some other code or command l...