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

Understanding ASCII / Unicode character encoding format

Computer stores everything in binary format. The process of converting a value into binary is called Encoding and the the process of converting value from binary is called Decoding. value --> binary  ::     Encoding  binary --> value   ::    Decoding for example: A number 12 is stored in its binary format as below.               12 ---> 00001100 but how are the characters are stored? If we want to store a character 'a' what formatting should be used ? It easiest way is to map the characters to number and store the binary format of that number. So, the character 'a' is represented as number 97 is stored as 1100001.  ASCII: In ASCII encoding format, 128 unique characters are identified and mapped with numbers (mostly English). One byte is used to represent all the characters. It uses 7 bits to represent numeric value 0 to 127 (total 128 characters).  The most significant bit is a...