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(){}
}
package college
class student
{
def studentmethod(){}
}
object Main
{
def main(args : Array[String])
{
val stu = new student()
val fac = new faculty()
}
}
|
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.
package bb
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.
package aa
object Main
{
def main(args : Array[String])
{
import bb.geek
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 May, 2019
Like Article
Save Article