Open In App

Packages In Scala

Improve
Improve
Like Article
Like
Save
Share
Report

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(){}
}




// 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.
    }
}


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.




// 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)
    }


Below is the example of Package using import clauses.




// 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();
    }
}


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
    


Last Updated : 28 May, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads