Scala/Import

Importation in Scala is a mechanism that enables more direct reference of different entities such as packages, classes, objects, instances, fields and methods. The concept of importing the code is more flexible in scala than Java or C++.
 * The import statements can be anywhere. It could be at the start of class, with in a class or object or with in method or block.
 * Members can be renamed or hidden while importing.
 * Packages, Classes or objects can be imported into the current scope.

Import
Any importation is defined using the keyword "import". A simple example of importing an object contained in a package:

In lines 1-3, a package named "p1" containing a class named "A" is declared. In lines 4-8, an object named "B" is defined. At line 5, a value named "x" is assigned a new instance of class "A" using the full identifier "p1.A" for class "A". At line 6, the keyword "import" is used, followed by the identifier "p1.A". The "import" declaration makes it possible for all following entities in the scope to refer directly to the last part of the identifier in the declaration, here the class "A". In the seventh line, the class "A" is referred to directly without having to use its full identifier.

Entities that can be imported includes members such as fields and methods of objects:

In the first line, the field named "Pi" of object "math" is imported, making it available to entities following the import declaration. In the second line, the method named "round" of object "math" is likewise imported. In the fourth and fifth line, "Pi" and "round" is referred to directly without the normal required identifiers "math.Pi" and "math.round".

The members of instances can also be imported:

In the first line a class named "Cube" with a value named "a" indicating side-length is declared. In lines 2-5, a method calculating a cube's volume is defined. At line 3, the field "a" of the argument named "cube" is imported, and in line 4 "a" is referred to directly. A new cube is instantiated in line 6, and its volume is printed in line 7.

Packages can also be imported:

In the first 3 lines, two nested packages named "p1" and "p2" are declared, with "p2" containing a class named "A". At the fifth line, "p2" is imported. At the seventh line, "p2" is referred to directly without having to qualify it with "p1.p2".

You can import multiple classes from a package in the following way. math.Pi and math.round classes are imported from the math package in the single import statement.

Wildcard
The _ character is used as the wild character in Scala. It is similar to the * character in Java. In the below example everything is imported from the math package.

Renaming
To avoid namespace collisions you may have to rename the members while importing into the scope. In the below example Pi is renamed as myPi and round as myRound while importing from the math package so that it doesn't collide with the local Pi and round members of the object.

Hiding
Scala provides an option to hide one or more classes while importing other members from the same package.

In the above example Pi is hidden and every thing else from the math package is imported into the scope. Accessing Pi in the example would give as an error as the Pi is hidden while importing.

Limiting the scope
You can use an import statement anywhere and this allows to control the scope of the imported members.

You can use the import statement at the top of the package.

In the above example math.pi is imported at the top of the package and scope of the math.pi is throughout the package. So it can be used in all the classes with in the package.

You can use import statement with in the class.

In the above example math.pi is imported within the class test1 and the scope of the Pi is limited within the class test1. and it can not be accessed from the class test2.

You can use the import statement within the method of class and limit the scope to that member.