How does ruby support multiple inheritance so that I can inherit from multiple classes?
In order to achieve multiple inheritance, Ruby provides something called mixins that one can make use of. Inheritance helps in improving the code reusability, as the developer won't have to create the same method again that has already been defined for the parent class.
Multiple inheritance - This is absolutely not possible in ruby not even with modules.
Inheritance is when a class inherits behavior from another class. The class that is inheriting behavior is called the subclass and the class it inherits from is called the superclass. We use inheritance as a way to extract common behaviors from classes that share that behavior, and move it to a superclass.
Multiple inheritance is a feature that allows one class to inherit from multiple classes(i.e., more than one parent). Ruby does not support multiple inheritance. It only supports single-inheritance (i.e. class can have only one parent), but you can use composition to build more complex classes using Modules.
Ruby does not directly have multiple inheritance. Ruby has something similar though: mixins. For example:
module M; end
module N; end
class C
include M
include N
end
C.ancestors #=> [C, N, M, Object, Kernel, BasicObject]
Note that mixins are not multiple inheritance, but instead mostly eliminate the need for it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With