Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
"Loosely coupled inheritance - interfaces - are considered a much better option these days."
That depends on whom you ask. I wouldn't agree, and neither would the people that made Python, Scala, etc. It's completely true that there are some issues with multiple inheritance. It's not just about tightly-coupled systems, but also about the greatly increasing possibility of name conflicts, etc. There can (depending on implementation) be issues like the dreaded diamond problem, etc.
Hmmm.. Lets see if this example makes sense.. "A" defines a method which returns the meaning of "Right" as in Will or Justice.. "B" inherits from A but overrides that method and returns the meaning as Direction.. Now, "C" inherits from A but overrides that method and returns the meaning as Correct.. Now, if D goes and inherits from both B and C (multiple inheritance).. there is no way to find which method to call if we want the meaning of right..
C++, provides multiple inheritance..
Java does not for the above reason.. :).. but you can achieve multiple inheritance through interfaces in java
C++ supports it; Java doesn't.
I think it's bad that Java doesn't support multiple inheritance, and I think that at times, that leads to bad design of certain aspects of Java programs. There are, however, certain difficulties with multiple inheritance that led the creators of Java to decide to not include that feature (look up "dreaded diamond problem" and the like).
Not supporting Multiple inheritance in Java is not bad at all. Multiple inheritance can be achieved in Java using interfaces.
C++ supports multiple inheritance of classes, Java (and C#) supports single inheritence of classes and multiple inheritance of interfaces.
- Selmeczy, Péter February 16, 2012When C++ added multiple-class inheritence pure OOP was thought to be "the" solution to everything.
In the meantime it was proven that class inheritance is good, but it ends up with tightly coupled systems. The more class inheritance you use, the more likely that one small change will have a huge impact on your entire system.
Loosely coupled inheritance - interfaces - are considered a much better option these days.