Does Ruby Support Single Inherita...
Does Ruby Support Single Inherita...
Ruby supports single inheritance, meaning each class can have only one parent class[1][3][4]. However, Ruby provides a way to simulate multiple inheritance using modules (also known as mixins)[1][2][3].
With single inheritance, a class can inherit methods and properties from one parent class. For example:
class Parent
def parent_method
puts "This is a parent method."
end
end
class Child < Parent
end
child = Child.new
child.parent_method # Output: This is a parent method.
In this case, Child
inherits from Parent
[4].
To achieve something similar to multiple inheritance, Ruby uses modules. A class can include multiple modules, allowing it to access methods defined in those modules[1][2][3]. This is known as a mixin. For example:
module A
def a_method
puts "This is a method from module A."
end
end
module B
def b_metho...
senior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào