Packages In Scala - GeeksforGeeks

28 May, 2019
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.
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 methodspackage x.y.z
// members of z
or can be used as:-package x
package y
package z
// member of z

package x.y.z
// members of z

or can be used as:-
package x
package y
package z
// member of z

Nesting packagespackage x{
   // members of x {as required}
   package y{
      // members of y{as required}
      package z{
         // members of z{as required}
      }
   }
}

package x{
   // members of x {as required}
   package y{
      // members of y{as required}
      package z{
         // members of z{as required}
      }
   }
}

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.
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 againpackage college class student{    def studentmethod(){}}   // Creating objectobject 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

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 directorypackage bb   // creating a classclass 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 directorypackage aa   // Creating objectobject 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();    }}
Importing all public members of package.import college._
//imports all college members students, faculties, houseKeeping etc.

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

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

import college.{student => stu}
//stu is used in-place of student

Picked
Scala
Scala-Packages
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
For Loop in Scala
Scala | flatMap Method
Scala | map() method
Scala List filter() method with example
Scala | reduce() Function
String concatenation in Scala
Type Casting in Scala
Scala Tutorial – Learn Scala with Step By Step Guide
Scala List contains() method with example
Scala String substring() method with example