
- Scala - Home
- Scala - Overview
- Scala - Features
- Scala - Environment Setup
- Scala - Build Tool (SBT)
- Scala - REPL
- Scala - Dot & Dotty
- Scala - Basic Syntax
- Scala - Hello World Program
- Scala - Identifiers
- Scala - Keywords
- Scala - Comments
- Scala - Code Blocks
- Scala - Semicolon
- Scala - Constructs
- Scala - Expressions
- Scala - Input and Output
- Scala - Optional Braces
- Scala - Underscore (_)
- Data Types and Variables
- Scala - Data Types
- Scala - Type Bounds
- Scala - Context Bound
- Scala - Variances
- Scala - Type Hierarchy
- Scala - Variables
- Scala - Variable Scopes
- Scala - Literals
- Scala - Numeric Types
- Scala - Boolean Types
- Scala - Char Type
- Scala - Unit Types
- Scala - Strings
- Scala - Arrays
- Scala - Null Type
- Scala - Nothing
- Scala - Any Type
- Scala - AnyRef Type
- Scala - Unified Types
- Scala - Dates and Times
- Scala - Ranges
- Scala - Multidimensional Arrays
- Scala - WrappedArray
- Scala - StringBuilder
- Scala - String Interpolation
- Scala - StringContext
- Scala - Type Casting
- Scala var vs val
- Scala Operators
- Scala - Operators
- Scala - Rules for Operators
- Scala - Arithmetic Operators
- Scala - Relational Operators
- Scala - Logical Operators
- Scala - Bitwise Operators
- Scala - Assignment Operators
- Scala - Operators Precedence
- Scala - Symbolic Operators
- Scala - Range Operator
- Scala - String Concatenation Operator
- Scala Conditional Statements
- Scala - IF ELSE
- Scala - IF-ELSE-IF-ELSE Statement
- Scala - Nested IF-ELSE Statement
- Scala Loop Statements
- Scala - Loop Statements
- Scala - while Loop
- Scala - do-while Loop
- Scala - Nested Loops
- Scala - for Loop
- Scala - break Statement
- Scala - yield Keyword
- Scala Classes & Objects
- Scala - Classes & Objects
- Scala - Constructors
- Scala - Auxiliary Constructor
- Scala - Primary Constructor
- Scala - This Keyword
- Scala - Nested Classes
- Scala - Getters and Setters
- Scala - Object Private Fields
- Scala - Singleton Object
- Scala - Companion Objects
- Scala - Creating Executable Programs
- Scala - Stateful Object
- Scala - Enumerations
- Scala - Polymorphism
- Scala - Access Modifiers
- Scala - Apply Method
- Scala - Update Methods
- Scala - UnapplySeq Method
- Scala - Inheritance
- Scala - Extending a Class
- Scala - Method Overloading
- Scala - Method Overriding
- Scala - Generic Classes
- Scala - Generic Functions
- Scala - Superclass Construction
- Scala Methods & Functions
- Scala - Methods
- Scala - Functions
- Scala - Methods vs Functions
- Scala - Main Methods
- Scala - Functions Call-by-Name
- Scala - Functions with Named Arguments
- Scala - Function with Variable Arguments
- Scala - Recursion Functions
- Scala - Default Parameter Values
- Scala - Functions without Parameters
- Scala - Implicit Parameters
- Scala - Higher-Order Functions
- Scala - Nested Functions
- Scala - Extension Methods
- Scala - Anonymous Functions
- Partially Applied Functions
- Scala - Lazy Val
- Scala - Pure Function
- Scala - Currying Functions
- Scala - Control Abstractions
- Scala - Corecursion
- Scala - Unfold
- Scala - Tail Recursion
- Scala - Infinite Sequences
- Scala - Dynamic Invocation
- Scala - Lambda Expressions
- Scala - Polymorphic Functions
- Scala Collections
- Scala - Collections
- Mutable and Immutable Collections
- Scala - Lists
- Scala - Sets
- Scala - Maps
- Scala - TreeMap
- Scala - SortedMap
- Scala - Tuples
- Scala - Iterators
- Scala - Options
- Scala - NumericRange
- Scala - Infinite Streams
- Scala - Parallel Collections
- Scala Advanced Types
- Scala - Union Types
- Scala - Intersection Types
- Scala - Type Aliases
- Scala - Structural Types
- Scala - Match Expression
- Scala - Singleton Type Operator
- Scala - Abstract Types
- Scala - Dependent Types
- Scala - Abstract Type Bounds
- Scala - Higher-Kinded Types
- Scala - Opaque Type Alias
- Scala - Path-Dependent Types
- Scala - Type Lambdas
- Scala - Type Inference
- Scala - Algebraic Data Types
- Scala Pattern Matching
- Scala - Pattern Matching
- Scala - Guards
- Scala - Variables in Patterns
- Scala - Type Patterns
- Scala - The Matchable Trait
- Scala - Matching Arrays
- Scala - Matching Lists
- Scala - Matching Tuples
- Scala - Exception Handling
- Scala - Extractors
- Scala - Pattern Bindings
- Scala - Regular Expressions
- Scala - Case Classes
- Scala - Partial Functions
- Scala - Packaging and Imports
- Scala - Implicit Imports
- Scala - Export Clauses
- Scala - Nested Packages
- Scala - Chained Packages
- Scala - Package Objects
- Scala Files I/O
- Scala - Files I/O
- Scala - Writing Files
- Scala - Listing Files
- Scala - Deleting Directories
- Scala - Check File Exists
- Scala Advanced Concepts
- Scala - Closures
- Scala - Futures
- Scala - Promises
- Scala - Traits
- Scala - Trait Mixins
- Scala - Layered Traits
- Scala - Trait Linearization
- Scala - Sealed Traits
- Scala - Transparent Traits
- Scala - Process Management
- Scala - Scaladoc
- Scala - Literal Type Arithmetic
- Scala - Inline keyword
- Scala - Def, Var & Val
- Scala - Dropped Features
- Scala Unit Testing
- Scala - Unit Testing
- Scala - uTest
- Scala - MUnit
- Scala - ScalaTest Runner
- Scala - ScalaMock
- Scala - JUnit
- Scala - Mocking
- Scala - BDD Testing
Scala - Chained Package
Scala provides chained packages for organizing code through packages. Chained packages are used to declare nested packages without using braces. So, you can write clean code.
Creating a Package
You can declare your packages in Scala at the top of a Scala file using the package keyword. These packages group related classes, traits, and objects together. So, you can organize code and control scope.
Basic Package Declaration
You can declare simple package in Scala like this -
package users class User
By convention, package names should be all lowercase. Directory structure should mirror the package hierarchy. For example, this is directory structure for the above package -
- ExampleProject - build.sbt - project - src - main - scala - users - User.scala - UserProfile.scala - UserPreferences.scala - test
So your code is organized. You can also group related classes together. Hence this is easy to manage and understand.
Chained Packages
You can declare multiple nested packages using a single line for each level of the package hierarchy.
Using Chained Packages
You can use sequence of package in chained packages to declare and create nested package structure without braces. For example -
package com.tutorialspoint.scala.packageimport class ExampleClass { def display(): String = "This is an example class in a chained package." }
This is clean and easy to define nested packages for more code readability.
Package Naming Conventions
You should use the organization domain name in reverse as part of the package name. It organizes packages and keeps a clean directory structure. For example -
package com.tutorialspoint.selfdrivingcar.camera class Lens
This corresponds to the following directory structure:
- SelfDrivingCar - src - main - scala - com - tutorialspoint - selfdrivingcar - camera - Lens.scala
It also avoids conflicts with packages from other organizations.
Importing in Chained Packages
Import statements bring required classes, traits, objects, and methods into scope. You can import classes from chained packages just like you do with flat packages.
Importing from Chained Packages
You can import a single class, multiple classes, and even all classes from a chained package. For example -
import com.tutorialspoint.scala.packageimport.ExampleClass import com.tutorialspoint.scala.packageimport._
Hence you can control which parts of a package are brought into scope and avoid namespace pollution.
Example of Using Chained Packages
Following is the example which shows you how to create and usage of chained packages -
Directory Structure
- ExampleProject - src - main - scala - com - tutorialspoint - scala - packageimport - ExampleClass.scala - AnotherClass.scala - Main.scala
ExampleClass.scala
package com.tutorialspoint.scala.packageimport class ExampleClass { def display(): String = "This is an example class in a chained package." }
This class has method to display a message.
AnotherClass.scala
package com.tutorialspoint.scala.packageimport class AnotherClass { def show(): String = "This is another class in the same chained package." }
This class has method to display a different message.
Main.scala
package com.tutorialspoint.scala.packageimport object Main { def main(args: Array[String]): Unit = { val example = new ExampleClass() val another = new AnotherClass() println(example.display()) println(another.show()) } }
Following is the above example which shows you how to use the classes within the chained package. It creates instances of ExampleClass and AnotherClass and calls their respective methods.
Command
Save the above programs in their respective files. The following commands are used to compile and execute the program.
> scalac com/tutorialspoint/scala/packageimport/*.scala > scala com.tutorialspoint.scala.packageimport.Main
Output
The output is -
This will produce the following result: This is an example class in a chained package. This is another class in the same chained package.
Chained Package Summary
- Scala uses chained packages to create namespaces. It modularizes code and prevents naming conflicts.
- Chained packages used to declare nested packages without using braces.
- Import statements can bring required classes, traits, objects, and methods from chained packages into scope.
- Chained packages provide greater control over scope and encapsulation. So, your code can be more modular and easier to maintain.