1

I made a class User (which is an instance of Class), and made it inherit from class Person. What makes this considered not to be multiple inheritance? Doesn't it inherit instance methods like new from Class and any methods that Person offers?

In other words, how is this not multiple inheritance:

class User < class Person
  some methods here...
end

when User already inherits all of Class's methods? Isn't the following what is going on under the hood?

class User < class Person and class Class
end
sawa
  • 165,429
  • 45
  • 277
  • 381
bkunzi01
  • 4,504
  • 1
  • 18
  • 25
  • 1
    Possible duplicate of [Multiple Inheritance in Ruby?](http://stackoverflow.com/questions/10254689/multiple-inheritance-in-ruby) – Wand Maker Jan 26 '16 at 14:26
  • Hey I edited my comment to be more specific. That post is more about specifying literally two parent classes whereas my question is how can any class in ruby list another class as a parent when it's parent looks like class "Class"... – bkunzi01 Jan 26 '16 at 14:33
  • 1
    Your code is not valid Ruby syntax. `User` is not a child of `Class`, it is an instance of `Class` class – Wand Maker Jan 26 '16 at 14:37
  • "Multiple inheritance" as a feature of a language is something exposed to users of that language. This is not actually useful as multiple inheritance to users of Ruby; you cannot inherit from two arbitrary classes. – user229044 Jan 26 '16 at 14:42
  • `new` is not an instance method. – sawa Jan 26 '16 at 16:02

2 Answers2

2

If you open irb you can check it yourself. Type User.superclass, you will see that User has only one superclass, which is Person. User will inherit the methods from Object because Person's superclass is Object

dgmora
  • 1,239
  • 11
  • 27
  • Thank you that really clears things up! What confused me is that I didn't realize everything fits nicely together in one big chain of command! Even though User and Person are instances of the class "Class", if you do Class.superclass you get Module and if you take the superclass of that you are back to Object! It all comes full circle!!! Thanks! – bkunzi01 Jan 26 '16 at 14:42
1

It is not "multiple inheritance" in usual way, but a simple chain of inheritance.

Ancestors cannot have own constructors unless you call them explicitly from initialize, and in complex hierarchies this can be very painful. The main point is that there's only one instance object, that is shared and methods are mixed into it.

In ruby class hierarchy is always a tree. Even mixins, that may look like a multiple inheritance - internally are implemented as inheritance chain.

True multiple inheritance can be hard, for example, in C++:

class A {}
class B: public A{}
class C: public A{}
class D: public B, public C{}

How many instances of A should be inside D? (the "diamond problem")

Ruby avoids it by simply not having the cause.

Vasfed
  • 18,013
  • 10
  • 47
  • 53