Method Visibility in Ruby
Last Updated :
06 Mar, 2020
Improve
Method visibility in Ruby refers that instance methods can be public, private or protected. Methods are by default public unless they are explicitly declared private or protected. Method visibility is used in Ruby to achieve Data Abstraction i.e showing only required information and hiding the background details.
Method visibility depends on the three types of access modifiers of a class in Ruby:
Public Access Modifier
Protected Access Modifier
Private Access Modifier
Ruby
Output:
Ruby
Output:
Ruby
Output:
Ruby
Output:
Public Access Modifier
The methods of a class which are public are easily called from any part of the program. Example :#1# program to illustrate public access modifier
# defining class
class Geeks
# methods are public by default
def publicMethod1
puts "publicMethod1 called!"
end
# using public keyword
public
def publicMethod2
puts "publicMethod2 called!"
end
end
# creating the object
obj = Geeks.new()
# calling methods
obj.publicMethod1()
obj.publicMethod2()
publicMethod1 called! publicMethod2 called!In the above, program two public methods
publicMethod1()
and publicMethod2()
of the class Geeks
are called.
Protected Access Modifier
The methods of a class which are declared protected can only be called from the class in which it is declared and the classes derived from it. Example :#2# program to illustrate protected access modifier
# super class
class Parent
# protected keyword
protected
# protected method
# can not be called directly
def protectedMethod
puts "protectedMethod called!"
end
end
# sub class
class Geeks < Parent
def publicMethod
# protected method called in public method
self.protectedMethod
end
end
# creating object
obj = Geeks.new
# calling method
obj.publicMethod
protectedMethod called!In the above program, the
protectedMethod()
of Parent
class is not accessible directly, so it is called from the publicMethod()
of the derived class Geeks
.
Private Access Modifier
The methods of a class which are declared private are called within the class only, private access modifier is the most secure access modifier. Example :#3# program to illustrate private access modifier
# defining class
class Geeks
# private keyword
private
# private method
# can not be called directly
def privateMethod
puts "privateMethod called!"
end
# public keyword
public
# public method
def publicMethod
# private method called in public method
privateMethod
end
end
# creating object
obj = Geeks.new
# calling method
obj.publicMethod
privateMethod called!In the above program, the
privateMethod()
of the Geeks
class an not be called directly. So it is called from the publicMethod()
of the class Geeks
.
Now let us look at another program which demonstrates Method Visibility.
Example :#4
# program to illustrate Method Visibility
# super class
class Parent
private
# private method
def privateMethod
puts "privateMethod called!"
end
protected
# protected method
def protectedMethod
puts "protectedMethod called!"
end
public
# public methods
def publicMethod1
puts "publicMethod1 called!"
end
def publicMethod2
# protected method called in public method
protectedMethod
# private method called in public method
privateMethod
end
end
# sub class
class Child < Parent
# public method
def publicMethod3
# protected method called in public method
protectedMethod
end
end
# creating object
obj1 = Parent.new
obj2 = Child.new
# calling method
puts "\nParent methods: \n"
obj1.publicMethod1
obj1.publicMethod2
puts "\nChild methods: \n"
obj2.publicMethod1
obj2.publicMethod3
Parent methods: publicMethod1 called! protectedMethod called! privateMethod called! Child methods: publicMethod1 called! protectedMethod called!