Home »
Ruby programming
Ruby Inheritance
Inheritance in Ruby: Here, we are going to learn about the inheritance in Ruby programming language with examples.
Submitted by Hrithik Chandra Prasad, on August 17, 2019
Inheritance in Ruby
Inheritance is a feature of Object Oriented languages in which new classes are derived from existing classes and resulting in the formation of a hierarchy of classes. The derived class is often called as child class and the existing class is termed as parent class. Inheritance provides code reusability which increases the human efficiency to write codes on the platform.
Ruby is an Object Oriented language, thus it supports the major feature of Inheritance. We can also explain inheritance via an example of two classes namely A and B.
Let us define these two classes in ruby using its syntax:
class A
#class methods
end
class B
#class methods
end
If we want to provide inheritance on class B, then the syntax will be changed as,
class A
#class methods
end
class B < A
#class methods
end
In the above syntax that we have used the "<" symbol to inherit a class. Now, if the object of class B is created then it will also be able to use the data members and member methods of class A. This provides code reusability as now we don't have to define methods which are already declared in class A, in class B as well. There are two classes possible after inheritance.
- Super Class: Super class is the Parent class whose methods are inherited. It can also be termed as Base class.
- Sub Class: Sub class is often termed as Derived or Child class. Sub class derives the methods and variables of Base class or Parent class.
Ruby supports only single level inheritance which means that a child class can have only one base class or parent class. It disallows Multi-level inheritance which means that if we want to make multiple parent classes of a single child class then it is not possible. Multiple inheritances are restricted because it creates ambiguity error or you can say that it creates multiple paths if the method name is same in both parent classes and the compiler could not decide or choose the right path.
Every class which is defined in Ruby platform has a default parent class. Before Ruby 1.9 version, every class has the parent class known as "Object class" by default but after Ruby 1.9 version, the parent class or the superclass of every class is "Basic Object class" by default.
Let us understand the practical implementation of Inheritance with the help of the following example,
=begin
Ruby program to demonstrate Inheritance.
=end
class ClassA
def Show
puts "Welcome to IncludeHelp"
end
def Message
puts "Enter your name: "
nm=gets.chomp
puts "Hello #{nm}, I hope you are doing great"
end
end
class ClassB<ClassA
def Hello
puts "Hello World!"
end
end
ob1=ClassB.new
ob1.Show
ob1.Message
ob1.Hello
Output
Welcome to IncludeHelp
Enter your name:
Hrithik
Hello Hrithik, I hope you are doing great
Hello World!
You can observe in the above code that, ClassB is the child class of Base class ClassA. The object of ClassB can access the methods of ClassA.