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
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() |
Output:
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 |
Output:
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 |
Output:
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.newobj2 = Child.new # calling method puts "\nParent methods: \n"obj1.publicMethod1 obj1.publicMethod2 puts "\nChild methods: \n"obj2.publicMethod1 obj2.publicMethod3 |
Output:
Parent methods: publicMethod1 called! protectedMethod called! privateMethod called! Child methods: publicMethod1 called! protectedMethod called!
Recommended Posts:
- Visibility of elements in bootstrap with Examples
- What is the difference between visibility:hidden and display:none ?
- HTML | DOM Style visibility Property
- Scala | Controlling visibility of constructor fields
- Hide or show HTML elements using visibility property in JavaScript
- Ruby | Set << method
- Ruby | Set & method
- Ruby | Set <= method
- Ruby | Set - method
- Ruby | Set > method
- Ruby | Set add() method
- Ruby | Set >= method
- Ruby | Set < method
- Ruby | Set + method
- Ruby | Numeric % method
- Ruby - Method Invocation
- Ruby | Numeric <=> method
- Ruby | Numeric + method
- Ruby | Date ajd() method
- Ruby | Numeric - method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

