Wednesday, July 25, 2018

Multiple Inheritance, Diamond problem

Programming languages like Java and Swift does not give Multiple Inheritance support cause of Diamond problem that may occur.

Diamond Problem:

         /   \
       B     C
        \      /

Class B & Class C inherits from Class A

A -> B & C

Class D inherits from Class B and Class C

B & C -> D

What If you have a overridden method in both class B & C from super class A?

As D is derived from B & C, There is problem when that method is called using D’s object.


b.method()  //overridden
c.method() //overridden

Now If I call,


Which one gets called? 

B’s method() or C’s  method()  ???

This is the Diamond problem.

That’s the reason, Multiple Inheritance is not supported in Swift.

If you try you end up with below compile time error.

Hope this post is useful. Feel free to comment incase of any queries.

No comments:

Post a Comment