Packages In Scala

Package in Scala is a mechanism to encapsulate a group of classes, sub packages, traits and package objects. It basically provides namespace to put our code in a different files and directories. Packages is a easy way to maintain our code which prevents naming conflicts of members of different packages. Providing access control to members of packages like private, protected, package specific controlling scope restricts the access of member to other packages, while the members with no modifier can be used inside any other package with some reference.

Declaration of Package

Packages are declared as a first statement at the top of a Scala file.
Syntax :



package package_name
// Scala classes
// traits
// objects..

Defining a package can be done in different ways:-

  • Chained methods
    package x.y.z
    // members of z
    

    or can be used as:-

    package x
    package y
    package z
    // member of z
    
  • Nesting packages
    package x{
       // members of x {as required}
       package y{
          // members of y{as required}
          package z{
             // members of z{as required}
          }
       }
    }
    

How Package works

Packages binds together the data in a single file or works as data encapsulation, when a file is saved it comes under default package or under a package name as specified at the top of the file. Package names and directory structure are closely related. For example if a package name is college.student.cse, then there will be 3 directories, college, student and cse. Such that cse is present in student and student is present in college.

college
     +student
          +cse

The idea is to make sure that files are easy to locate in directories while using the packages.
Package naming conventions for domain names are in reverse order as i.e. org.geeksforgeeks.practice, org.geeksforgeeks.contribute.

Adding Members to Packages

We can add any numbers of members in a package such as classes, subclasses, traits, object containing the main method and sub packages. Unlike the java packages we can add a declared packages in different scala files i.e. different scala files can be written can for the same package.
Example:

// file named as faculty.scala
package college
class faculty{
   def faculymethod(){}
}
filter_none

edit
close

play_arrow

link
brightness_4
code

// file named as student.scala
// containing the main method
  
// using the college package name again
package college 
class student
{
    def studentmethod(){}
  
// Creating object
object Main
{
  
    // Main method
    def main(args: Array[String])
    {
        val stu= new student()
        val fac= new faculty() 
        // faculty class can be accessed while
        // in different file but in same package.
    }
}

chevron_right


What is actually created in directory structure is as below:-

college
     +faculty.scala
     +student.scala

Using Packages

Packages can be used by different ways in a program. Import clauses are quite flexible in Scala than in java. Such as import clauses can be used any where in the program as an independent statement in the program using the keyword import, Java does not allow that.

filter_none

edit
close

play_arrow

link
brightness_4
code

// base.scala
// bb directory
package bb 
  
// creating a class
class geek
    private var id=0
    def method()
    {
        println("welcome to geek class")
        println("id="+id)
    }

chevron_right


Below is the example of Package using import clauses.

filter_none

edit
close

play_arrow

link
brightness_4
code

// main.scala
// aa directory
package aa 
  
// Creating object
object Main
{
    // Main method
    def main(args: Array[String])
    {
        // importing in main method
        import bb.geek 
          
        // using the member injected using import statement
        val obj = new geek() 
        obj.method();
    }
}

chevron_right


Different ways to use import statements

  • Importing all public members of package.
    import college._
    //imports all college members students, faculties, houseKeeping etc.
    
  • Imports only selected members of a package.
    import college.{faculty, houseKeeping}
    //member student is not selected hence cannot be used in current file
    
  • Imports and rename the members.
    import college.{student => stu}
    //stu is used in-place of student
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.