Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Inheritance in Ruby?

I thought that Ruby only allowed single inheritance besides mixin. However, when I have class Square that inherits class Thing, Thing in turn inherits Object by default.

class Thing end  class Square < Thing end 

Doesn't this represent multiple inheritance?

like image 667
Bhubhu Hbuhdbus Avatar asked Apr 20 '12 23:04

Bhubhu Hbuhdbus


People also ask

Does Ruby support multiple inheritance?

Ruby supports only single class inheritance, it does not support multiple class inheritance but it supports mixins.

What is mixin Ruby?

Mixins in Ruby allows modules to access instance methods of another one using include method. Mixins provides a controlled way of adding functionality to classes. The code in the mixin starts to interact with code in the class. In Ruby, a code wrapped up in a module is called mixins that a class can include or extend.

What do you mean by multiple inheritance?

Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit features from more than one parent object or parent class. It is distinct from single inheritance, where an object or class may only inherit from one particular object or class.

What is super class in Ruby?

Ruby uses the super keyword to call the superclass implementation of the current method. Within the body of a method, calls to super acts just like a call to that original method. The search for a method body starts in the superclass of the object that was found to contain the original method.


1 Answers

I think you are taking the meaning of multiple inheritance in a wrong way. Probably what you have in mind as multiple inheritance is like this:

class A inherits class B class B inherits class C 

If so, then that is wrong. That is not what multiple inheritance is, and Ruby has no problem with that. What multiple inheritance really means is this:

class A inherits class B class A inherits class C 

And you surely cannot do this in Ruby.

like image 107
sawa Avatar answered Sep 28 '22 20:09

sawa