Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
Scala Long >(x: Int) method
Next article icon

Controlling Method Scope In Scala

Last Updated : 06 Nov, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
As the name suggests Access Modifiers in scala helps to restrict the scope of a class, variable, method or data member. Controlling Method Scope In Scala helps to restrict the scope of method or data member. There are five types of controlling method scope in Scala:
  1. Public Scope
  2. Private Scope
  3. Protected Scope
  4. Object-private Scope
  5. Package Specific
Public Scope
  • When no access modifier is specified for a class, method or data member, it is said to be having the default access modifier by default.
  • The data members, class or methods which are not declared using any access modifiers i.e. having default access modifier are accessible anywhere using package & imports or by creating new instances. Example : Scala
    // Scala program of Public Scope // package testA class classA {     def method1(): Unit=     {         println("method1")     } }  // Creating object object GfG {      // Main method     def main(args: Array[String])     {         // classA in the same package          // as the main method          var x = new classA         x.method1()     } } 
    Output :
      method1  
    Example :
Private Scope
  • Private modifier is same as private in java. By marking a method or variable private it is available to the current class and its members and any of instances of the same class.
  • Any other object/class of same package will not be able to access the private members.
  • This is done by using the private access modifier. Example : Scala
    // Scala program of Private Scope // package testA class classA  {     var x = 1     private def method1: Unit =     {         println("method1")     } }  // Creating object object GfG {     // Main method     def main(arg: Array[String])     {         var obj1 = new classA         printf("x = "+obj1.x)         // println(obj1.method1) error: method          // method1 in class classA cannot          // be accessed in classA     } } 
    Output:
      x = 1  
Protected Scope
  • Scala protected is different from protected in java. To mark a member protected, use the keyword protected before a class or variable.
  • Protected members can be accessed only by the sub classes in the same package. Example : Scala
    // Scala program of Protected Scope // package test class classab {     protected var ab: Int=4     var ad: Int =1 }  // Creating object object GfG extends classab {      // sub class     // Main method     def main(args: Array[String])     {         println(ab) //can be accessed         println(ad) //can be accessed     } } 
    Output:
      4  1  
  • Protected members cannot be accessed by other members in other packages even with imports. Example : Scala
    // Scala program of Protected Scope // package testA package testA {     class classA      {         protected var ab: Int=4         var ad: Int =1     } }  // another package testB package testB {     // importing all the members      // from testA package     import testA._          // Creating object     object GfG     {         // Main method         def main(args: Array[String])         {             var ta= new classA             ta.ad             ta.ab //error         }     } } 
    Output:
    error: variable ab in class classA cannot be accessed in testA.classA Access to protected method ab not permitted because enclosing object GfG in package testB is not a subclass of class classA in package testA where target is defined ta.ab //error ^ one error found
Object Private/Protected Scope
  • Object private is same as private the only difference is that the member declared object private will available only from in which the member is defined, i.e. no object can access it hence therefore named object private.
  • Object protected is same as protected the only difference is that the member will be only available in which it is defined or to the sub classes and not available to the objects.
  • To mark an member object private use the keywords private[this].
  • To mark an member object protected use the keywords protected[this], where this refers or points to the current object. Example : Scala
    // Scala program of Object Private/Protected Scope // package test1.test11 class class11  {     private[this] var x = 1     private var t = 2     var z = 3     def method11(other: class11): Unit =     {         println(x)         println(t)         println(z)                  // println(other.x)         println(other.t)         println(other.z)     } } // here on line14 x can only be // accessed from inside in which  // it is defined  // Creating object object GfG {     // Main method     def main(arg: Array[String])     {         var obj11 = new class11() //current instance created         var y = 2         println(obj11.method11(obj11))         println(obj11.z)         //println(obj11.t) //error: t cannot be accessed         //println(obj11.x) //error: x is not a member of class11         //according to obj11 x is not a member     } } 
    Output :
      1  2  3  2  3  ()  3  
Package Specific
  • When we want a member to be available to a whole package. It comes to declare that member as private[package_name].
  • All the member inside the package can access that member.
  • Member can be accessed by any other package whose name is being qualified to. Example : Scala
    // Scala program of Package Specific // Scala program of Package Specific package aa class geek {     class g1     {           // inner class         // private to class g1         private var a = 0                   // available to package aa         private[aa] var b = 0          def method()         {             a = a + 1             b = b + 1             println("welcome to inner class g1")             println("a= "+a)         }     } }   // Creating object object Main {     // Driver code     def main(args: Array[String])     {         val obj = new geek()         val o = new obj.g1         o.method();         println("b= "+o.b);     } } 
Output :
welcome to inner class g1  a= 1  b= 1  

Next Article
Scala Long >(x: Int) method

P

Patabhu
Improve
Article Tags :
  • Scala
  • Scala
  • Scala-Method

Similar Reads

  • Methods to call on a Set in Scala
    A set is a collection that only contains unique items. In Scala, both mutable and immutable sets are available. The mutable set is those set in which the value of the object is change, but, in the immutable set, the value of the object is not changed itself. The immutable set is defined under Scala.
    13 min read
  • Scala Long >=(x: Long) method
    In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The >=(x: Long) method is utilized to return true if this value is greater than or equal to x, false otherwise. Method Definition - def >=(x: Long): Boolean Returns - Returns true if this value is gre
    1 min read
  • Scala Double >=(x: Int) method
    In Scala, Double is a 64-bit floating point number, which is equivalent to Java’s double primitive type. The >=(x: Int) method is utilized to return true if this value is greater than or equal to x, false otherwise. Method Definition - def >=(x: Int): Boolean Returns - Returns true if this val
    1 min read
  • Scala Long >=(x: Int) method
    In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The >=(x: Int) method is utilized to return true if this value is greater than or equal to x, false otherwise. Method Definition - def >=(x: Int): Boolean Returns - Returns true if this value is great
    1 min read
  • Scala Long >(x: Int) method
    In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The >(x: Int) method is utilized to return true if this value is greater than x, false otherwise. Method Definition - def >(x: Int): Boolean Returns - Returns true if this value is greater than x, fal
    1 min read
  • Call a method on a Super Class in Scala
    This concept is used when we want to call super class method. So whenever a base and subclass have same named methods then to resolve ambiguity we use super keyword to call base class method. The keyword "super" came into this with the concept of Inheritance. Below is the example of call a method on
    2 min read
  • Scala Stack contains() method with example
    In Scala Stack class, the contains() method is utilized to check if an element is present in the stack of not. Method Definition: def contains(elem: A): Boolean Return Type: It returns true if the element is present in the stack or else it returns false. Example #1: // Scala program of contains() //
    1 min read
  • Scala Long >=(x: Char) method
    In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The >=(x: Char) method is utilized to return true if this value is greater than or equal to x, false otherwise. Method Definition - def >=(x: Char): Boolean Returns - Returns true if this value is gre
    1 min read
  • Scala SortedMap contains() method with example
    The contains() method of Scala is equivalent to the isDefinedAt method of Scala but the only difference is that isDefinedAt is observed on all the PartialFunction classes while contains is clearly defined to the SortedMap interface of Scala. It checks whether the stated SortedMap contains a binding
    2 min read
  • Scala Set count() method with example
    The count() method is utilized to count the number of elements in the set. Method Definition: def count(p: (A) => Boolean): Int Return Type: It returns the number of elements present in the set. Example #1: // Scala program of count() // method // Creating object object GfG { // Main method def m
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences