Skip to main content

Ruby on Rails Part 2 - Class modifiers and inheritance

 Ruby on Rails Part 2 - Class modifiers and  inheritance


Table of content

  1. Access specifiers
  2. Inheritance
  3. Method overriding
  4. Super method

Access specifiers

Public, Private, Protected 

  • Access modifiers are only applicable on methods of a class

  • By default, all methods are public

  • We can’t apply any access control to the data members and the class variables

  • Class variables and data members are always private

There are 3 types of access modifiers :

Public

These methods can be accessed outside of the class definition. By default all the methods are public.

 
class Base
@id = 2
public
def method1()
puts "Inside method1"
end
end

obj = Base.new
obj.method1() # method1 is public hence can be called outside class

Protected

These methods can only be called from within the class or its subclass definition.

 
class Base
@id =

def method1()
puts "Inside method1"
method2()
end

protected

def method2()
puts "Inside method2"
end
end

obj = Base.new
obj.method1() # allowed as method2 (protected method) is called using method1 (public method)
obj.method2()  # not allowed outside class as method2 is protected
  

Output 

 
Inside method1
Inside method2
main.rb:18:in `<main>': protected method `method2' called for #<Base:0x00561174eddd80> (NoMethodError)
Did you mean? method
methods
method1

Private

These methods cannot be accessed outside of the class definition. A private method in Ruby means the method cannot be called with an explicit receivers. Not even with the self. self must be implicit receiver, and that self must be an instance of current class.

 
class Base
@id = 2
def method1()
puts "Inside method1"
method2()
end
private
def method2()
puts "Inside method2"
end
end

obj = Base.new
obj.method1() # Allowed because method2 is called indirectly from within
obj.method2()  # not allowed outside class as method2 is private
 

Output 

 
Inside method1
Inside method2
main.rb:18:in `<main>': private method `method2' called for #<Base:0x00561174eddd80> (NoMethodError)
Did you mean? method
methods
method1

Inheritance 

Ruby supports only one type of inheritance: single inheritance (A -> B). This can be extended to multilevel inheritance (A->B->C). Inheritance can be achieved using the following syntax:

ChildClassName < ParentClassName

 
class ParentClass #Parent Class
def initialize
puts "This is Parentclass constructor"
end

def super_method
puts "Method of Parentclass"
end
end

class ChildClass < ParentClass #Childclass inheriting parentclass
def initialize
puts "This is Childclass"
end
end

child_obj = ChildClass.new
child_obj.super_method #Child object calling parent method


Method Overriding

Child class override with the methods of the Parent class.

 
class Parent
def parentMethod
puts "Parent"
end
end

class Child < Parent
def parentMethod # Redefining same class that is in parent
puts "Child"
end
end

obj = Child.new
obj.parentMethod # Prints Child because Parent method has been overridden

Super Method

Super method call, calls the method of same name in the parent class. Below example explain display method call to the parent class.

 
class Parent
def display a=0, b=0
puts a, b
end
end

class Child < Parent
def display a, b
super # Passes both incoming args a and b
super a # Passes only a
super a, b # Passes both incoming args a and b
super() # Doesn’t pass anything
end
end

obj = Child.new
obj.display(8, 9
 

 Output 

 
8 9
8 0
8 9
0 0
 

Thank you folks. If you like this Ruby on Rails post, please do checkout my other post on  Django with Python 3 series, Ruby on Rails

Most viewed

Ruby on rails part 6 - Blocks , lambda, procs and closure

 Blocks , lambda, procs  and closure Table of content  1. Blocks 2. Lambda 3. Procs 4. Closure Blocks  Ruby blocks are little anonymous functions that can be passed into methods. Blocks are enclosed in a do-end statement or between brackets {} 
. Blocks can have multiple arguments
. The argument names are defined between two pipe | characters. Blocks are typically used with ‘each’ method which iterates over a list. Syntax of block using {} ['List of items'].each { | block arguments|  block body }  Syntax of block using do-end ['List of items'].each do | block arguments |      # block body end Example of block declared as do-end with each method.   [ 1 , 2 , 3 ].each do |num| puts num end     Output   $ ruby block_with_each.rb 1 2 3 $    Blocks can also be saved in variables or passed as argument to another function.   yield is a Ruby keyword that is used to call a block. When you use the yield keyword, the code inside the block will run. Example of saving a bl