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 4 - Exception Handling

  Ruby on Rails Part 4 - Exception Handling  Table of content Exception Handling retry raise ensure else  catch and throw Exception classes Exception Handling Enclose the code that could raise an exception with a begin/end block and use rescue clauses to tell Ruby the types of exceptions that you want to handle. The syntax for exception handling : 
 begin  
      #- statements
 rescue OneTypeOfException       #-
 handle the exception rescue AnotherTypeOfException       #- 
handle the exception else       
# Other exceptions
 ensure 
      # ensure block is always executed
 end 
 Everything from begin to rescue is protected 

in the block. 
If an exception occurs during the execution of this block of code, control is passed to the 

block between rescue and end.
 
For each rescue clause in the begin block, Ruby compares the raised Exception against each 

of the parameters of the...