Packages and Interfaces  «Prev  Next»

Lesson 3Using packages
ObjectiveImport Classes from a Package

Import Classes from a Package

Public Java classes are always visible if you use their fully qualified name. The import statement, however, makes Java classes available to the current class with the use of a shortened name. Importing a class does not make it visible, it saves you the task of repeatedly typing the fully qualified name.

There are two forms of the import Statements

Java import Statement

import java.applet.Applet;
import java.net.*;

  1. Line 1: Imports the Applet class from the java.applet package.
  2. Line 2: Imports all of the classes in the java.net package.

Using Packages in Java

To make types easier to find and use, to avoid naming conflicts, and to control access, programmers bundle groups of related types into packages. A package is a grouping of related types providing access protection and name space management. Note that types refers to classes, interfaces, enumerations, and annotation types. Enumerations and annotation types are special kinds of classes and interfaces, respectively, so types are often referred to in this lesson simply as classes and interfaces.


You can import classes either way, I find it more convenient to import all of the classes in a package using the * wildcard. Please refer to the Java API documentation if you have a question about any of the standard Java classes or packages.
The Java API documentation is available for download at Oracle JDK 17.

Rules to Remember about Java Packages

A few of important rules about packages:
  1. Per Java naming conventions, package names should all be in lowercase.
  2. The package and subpackage names are separated using a dot (.).
  3. Package names follow the rules defined for valid identifiers in Java.
  4. For packaged classes and interfaces, the package statement is the first statement in a Java source file (a .java file). The exception is that comments can appear before or after a package statement.
  5. There can be a maximum of one package statement per Java source code file (.java file).
  6. All the classes and interfaces defined in a Java source code file will be defined in the same package. There is no way to package classes and interfaces defined within the same Java source code file in different packages.

Quiz Question regarding Java Packages

Given the following.
package p1;
public class Fruit{ 
 protected String taste; 
 protected void changeColor() {} 
}

package p2; 
public class Apple extends Fruit { 
// place 2 options here
}


Which of the following may be legally placed inside the class Apple? (Select two choices.)
  1. protected int changeColor(){}
  2. { taste="sweet"; }
  3. void changeColor() {}
  4. { (new Fruit()).taste="sweet"; }
  5. protected void changeColor() throws RuntimeException

Answer: B, E

Explanation:
The protected variable taste in the class Fruit is available in class Apple even though it is in a different package, since Apple extends Fruit.
However, choice D will not compile because it accesses the field taste of a Fruit instance, and Fruit is the parent class of Apple. Choice E is correct because there is no restriction on throwing a RuntimeException when overriding methods.
Choice A will not compile because the return type is different for the changeColor method in option A, although the argument list matches.
Choice C will not compile because a protected method cannot be overridden by a method with default access.
The correct answer is: B.{ taste="sweet"; }, E. protected void changeColor() throws RuntimeException

Import Keyword - Exercise

Click the Exercise link below to import classes from packages.
Import Keyword - Exercise