Ruby Inheritance

Inheritance is a way to acquire properties of one class to other class. Inheritance are provide an ability to reuse features of existing class. That is most important feature of object oriented programming.

DervideClass < BaseClass

Terminology of Inheritance

When inherits one class properties to another class there are following terminology are used.

Base/Supper class: Base class is a class which are provide the functionality to other class. or other word base class is a class which are inherited by other class and which are used their properties. This is also known as parent class.

Derived Class/Sub Class: This class are use the properties of other class. And implements new features and functionality. This is also known as child class. Ruby programming language are support only single base class inheritance. There is not allowing to use multiple inheritance directly. Multiple inheritance is achieved by mixins designing interface.

Let's see one basic example how to inherit one class properties to another class. And how to access base class properties using derived class objects

#Base class
class B
  #Base class constructor 
  def initialize
    puts "Class B initialize"
  end
  def status
    puts "Class B is Active"
  end
end
#class D are inherited the properties of class B
# D is derived class
class D < B
  def initialize
    puts "Class D initialize"
  end
  def info
    puts "Class D info method"
  end
end
#Make object of class D
obj=D.new
obj.status #access base class method
obj.info

In this example class B is represent as Base class of class D. class D object is capable to access the properties of base class and derived class (itself). Output of this program is.

Class D initialize
Class B is Active
Class D info method

Above example are very simple here derived class object used the method of base class. Let's view one more example to understand other basic concepts.

#Base class
class Base

  #Base class constructor 
  def initialize(x,y)
    @x=x
    @y=y
  end
  def data_info
    puts "Base class Data"
    puts "x : #{@x}  y: #{@y}"
  end
  
end
# class Derived are inherited the properties of class Base
# Derived is derived class derived
class Derived < Base
  def initialize(x,y,z)
    #execute Base class constructor
    super x,y
    @z=z
  end
  def details
    puts "Derived class Data"
    puts "x : #{@x}  y: #{@y}  z : #{@z}"
  end
end
#Make object of class Derived
obj=Derived.new(1,2,3)
obj.data_info #access Base class method
obj.details

Note that in this example derived class constructor are executing base class constructor using super class keyword. super keyword can be access the properties of base class. In this example they are set initial values of @x and @y of base class data member.

Inherited base class properties

Note that in Derived class are only one instance variable @z, but this class are inherited the properties of Base class and super are execute base class constructor. That the reason base class instance variable are added to instance of derived class. And derived class object are access all three instance variable (@x,@y,@z). Output of this program are as follows.

Base class Data
x : 1  y: 2
Derived class Data
x : 1  y: 2  z : 3

At this moment there can be many doubt in you'll mind.

1) Base class instance variable are added to derived class instance when executed base class constructor by super statements.

2) In case derived class constructor not execute base class constructor in this situation base class instance variable not added to derived class instance. In this situation derived class object is capable to access the public method of base class. and class variable of base class are added to derived class.

#Base class
class Base
  #Class variable 
  @@data1="A"
  @@data2="B" 
  #Base class constructor 
  def initialize(x,y)
    #Set instance variable
    @x=x
    @y=y
  end
  def base_info
    puts "Start base_info"
    puts defined? @x  #nil
    puts defined? @y  #nil
    puts "End base_info"
  end
end
# Derived class are inherited the properties of class Base
# Derived is derived class derived
class Derived < Base

  def initialize(z)
    #When not assign the initial value to base class instance variable
    # super 1,2
    @z=z
  end
  def derived_info
    puts "Start derived_info"

    puts defined? @@data1 
    puts defined? @@data2
    puts defined? @z
    puts defined? @x #nil not
    puts defined? @y #nil not
    #access class variable of base class
    puts "@@data1 : #{@@data1}"
    puts "@@data2 : #{@@data2}"
    puts "End derived_info"
  end
end
#Make object of class Derived
obj=Derived.new(3)
obj.base_info #access Base class method
obj.derived_info

Add Base class variable to derived class
Start base_info


End base_info
Start derived_info
class variable
class variable
instance-variable


@@data1 : A
@@data2 : B

If derived class methods are modified the value of base class variable in this situation base class variable also modified. See this example.

#Base class
class Base
  #Class variable 
  @@data1="A"
  @@data2="B" 
  def base_info
   puts "Base_info method"  
   puts "@@data1 :  #{@@data1}"
   puts "@@data2 :  #{@@data2}"
  end
end
# Derived class are inherited the properties of class Base
# Derived is derived class derived
class Derived < Base

  def derived_info
    puts "Derived_info method"  
    puts "@@data1 : #{@@data1}"
    puts "@@data2 : #{@@data2}"
  end
  def set_new(x,y)
    @@data1=x
    @@data2=y
  end
end
#Make object of class Derived
objOne=Derived.new

puts "Inital values of objOne class variable"
objOne.base_info #access Base class method
objOne.derived_info

objOne.set_new(1,2)

puts "After set values of objOne class variable"
objOne.base_info #access Base class method
objOne.derived_info


objTwo=Base.new
puts "Inital values of objTwo class variable"
objTwo.base_info 
change value of class variable
Inital values of objOne class variable
Base_info method
@@data1 :  A
@@data2 :  B
Derived_info method
@@data1 : A
@@data2 : B
After set values of objOne class variable
Base_info method
@@data1 :  1
@@data2 :  2
Derived_info method
@@data1 : 1
@@data2 : 2
Inital values of objTwo class variable
Base_info method
@@data1 :  1
@@data2 :  2


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved