Inheritance is one of the great feature in Object Oriented Programming. Ruby supports inheritance. Inheritance allows to inherit the features of the base classes to its derived classes. That means, through derived class objects we can access the features of the base class.
Through this Article, we will discuss how we use Inheritance in Ruby. Let’s start with below simple example;
class Base def SayHello print "Hello! from Base class\n" end end class Derived < Base def SayHi print "Hi! from Derived class\n" end end obj = Derived.new obj.SayHi obj.SayHello
Above, we have created two classes; one is Base class and another one is Derived class. Base class has the method SayHello defined and whereas Derived class has the SayHi method. Both these methods; displays some message on the display.
Derived class is derived from the Base class using the less-than symbol “<“; which instructs to the Ruby that the left side class is inheriting from the right side class. That allows, to access the members of the Base class from it’s inherited class.
After that, created an instance of Derived class. Through the instance of the class; we have called the methods of both these Classes. The method calls were succeeded; due the relationship between these two classes; otherwise, Ruby will throw the Error(s), if we attempt to call one class method(s) using other class’s object.
Let’s take another example, where we will take instance variables of the class; and attempt to access base class variable(s) in it’s derived class.
class Base
def initialize
@base_var = 100
end
end
class Derived < Base
def initialize
super # try without adding this line and observe the results
@derived_var = 200
end
def PrintMe
puts @base_var
puts @derived_var
end
end
obj = Derived.new
obj.PrintMe
From above, both the Classes have variables defined. Both these variables are initialized through initialize
method(s). Through the PrintMe method of Derived class; we have accessed the variable(s) defined in Base class. That proves that, the members of the base class can be accessible in derived classes.
Why super
was used in above code?
This is important here to use. super
keyword instructs to use base class members; in this case, it calls Base‘s initialize
method in Derived‘s initialize
method, before the derived class members are initialized. This is same as super.initialize
statement. Ruby doesn’t initialize base class automatically; hence we must have to instruct to initialize base class members by instructing through the super
keyword. Other languages, such as, in C++; the compiler automatically calls base class constructors before creating the derived class objects.
Without initializing base class members, if you start using derived class methods; you may see no or unexpected results.
How about Multiple Inheritance?
So far, we have discussed about Single Inheritance in Ruby. Multiple Inheritance means, derive the Class from multiple Classes. That means, the derived class has multiple base classes. Ruby, doesn’t support this in Class level. Hence we have to stick to Single Inheritance only in Class level.
| Nick |