Reputation: 3025
I'm confused about an OOP feature, multiple inheritance. Does OOP allow Multiple Inheritance? Is Multiple Inheritance a feature of OOP? If Multiple Inheritance is a feature then why don't languages like C#, VB.NET, java etc. support multiple inheritance? But those languages are considered as strongly supported OOP language. Can anyone address this question?
Upvotes: 1
Views: 2196
Reputation: 341
Java
Multiple inheritence in Java is quite possible. But there are certain restrictions that Java has.
Java classes do not support multiple inheritances but java interfaces do support multiple inheritances
Java Classes
public class elderchild
{
\\elder child attributes
}
public class middlechild
{
\\middle child attributes
}
public class parent extends middlechild, elderchild \\this is wrong, multiple class inheritance is restricted
{
\\parent attributes
}
Java Interfaces
public interface animal
{
\\animal attirbutes
}
public interface mammal
{
\\mammal attributes
}
public class dog implements animal, mammal \\this is correct!
{
\\dog attributes
}
Note: The diamond problem can also occur in java. To read more about it, open: https://stackoverflow.com/questions/29758213/multiple-inheritance-ambiguity-with-interface#:~:text=Java%20doesn't%20support%20multiple,methods%20will%20have%20same%20signature.
Upvotes: 0
Reputation: 178
Languages like Java and C# , which is highly inspired from java as matter of Object Oriented Principles, are built for application making development,so the designers of these kind of languages decided to take approach in which OOP can be understandable and quickly be learnt by developers.So, for the sake of simplicity and clarification of responsibilities of each class in inheritance, they avoid considering one object inherit from multiple objects.Instead they consider using Interfaces in order to implement multiple very different behaviors and attributes.With this in mind the principles of: Single responsibility of each object ,and transparent segregation of responsibility of each interface and subsequent inherited objects be fully understandable.
Upvotes: 3
Reputation: 89729
First, you have to distinguish between multiple inheritance and multiple supertypes, these are two very different things.
Multiple inheritance usually reflects to an actual inheriting of implementation (like class inheritance in most OOP languages) and presents a variety of concerns. One is conflict between names and implementations (e.g., two methods with same name and a different implementation), and then issues like the idamond problem.
Multiple supertypes usually refers to the ability to check types (and in some cases cast), and usually does not involve inheriting implementations. For example, in Java you have interfaces that merely declare your methods. So your subtype supports the union of the method supported by the supertypes. This presents less problems because you do not have a method with multiple implementations.
Multiple inheritance usually involves multiple supertypes, though some languages like C++ allow you modify the visibility of this fact (e.g., who can know that type B is a subtype of type A).
To the best of my knowledge, there is no requirement for an OOP language to support either, but at least multiple-supertypes is necessary for a usable OOP language where most design patterns can be implemented in a straightforward way. Multiple inheritance, IMHO, is really not that useful to justify the complexity and costs. I've made the switch to Java ten years ago and can't say that I missed it too much.
Upvotes: 0
Reputation: 369438
Inheritance doesn't have anything to do with object orientation. There's plenty of OO languages that do not support inheritance at all and there's plety of non-OO languages that do support inheritance. Those two things are completely orthogonal.
Upvotes: 4
Reputation: 10392
Multiple inheritance refers to a feature of SOME object-oriented programming languages, not all of them.
These other languages you are referring to use interfaces.
Upvotes: 0
Reputation: 74250
There is no requirement in OO to support multiple inheritance, which is supported by languages such as C++. C# and Java don't support and they are no less OO because of that.
Upvotes: 4