Inheritance is the relationship between two classes. Inheritance creates parent relationships between classes. It is a mechanism for code to reuse and allow independent extensions to source software through public classes and interfaces. The advantage of inheritance is that the classes below the hierarchy receive the functions of those above, but can also add their own features.
In Ruby, a class can inherit only one class. (that is, a class can inherit from a class that inherits from another class that inherits from another class, but one class cannot inherit from many classes at once). The BasicObject class is the parent class of all classes in Ruby. Therefore, its methods are available for all objects if they are not explicitly redefined.
Ruby overcomes one-time inheritance right away with mixin.
I will try to explain with an example.
module Mux def sam p "I am an module" end end class A include Mux end class B < A end class C < B end class D < A end
You can track using class_name.superclass.name and execute this process if you did not find BasicOject in this hierarchy. BasicObject is a super class class for each class. suppose we want to see a tree of class hierarchy C.
C.superclass => B B.superclass => A A.superclass => Object Object.superclass => BasicObject
You can see the entire hierarchy of class C. Note that using this approach, you will not find modules that are included or added to parent classes.
There is another approach for finding a complete hierarchy, including modules. According to the Ruby ancestors document. Returns a list of modules included / added to mod (including the mod itself).
C.ancestors => [C, B, A, Mux, Object, Kernel, BasicObject]
Here, Mux and Kernel are modules.
http://rubylearning.com/satishtalim/ruby_inheritance.html https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)
Mukesh Kumar Gupta Jun 06 '17 at 12:19 2017-06-06 12:19
source share