CHAPTER 5: Introduction to Java: Objects, Methods, Classes, and Interfaces 149
A Java interface cannot use any of the other Java access control modifier keywords, so it cannot be
declared as private or protected. We’ll be learning about these access control modifiers in a future
section of this chapter.
It is important to note that only the methods declared in the interface absolutely need to be included.
The data fields that I have at the top of the class definition are optional and are in this example to
show its parallel to the Car class that we declared earlier without using an interface. There is not
much difference other than using the implements keyword, except that implementing an interface
tells the compiler to check and make sure that all of the necessary methods that make a Car class
complete (work properly) are included by the developer.
Logical Collection of Classes: Using a Package
As you know, each time you define a new project in Android, the Eclipse Integrated Development
Environment (IDE) will create a package to contain your own custom classes, which you will define
as you implement your application’s custom functionality. In your HelloUniverse Android application,
which you created back in Chapter 3, you named your package as absolute.beginner.hellouniverse.
If you remember, that first dialog in the New Android Application Project series of dialogs asks
you to specify this package name (refer to Figure 3-3 if you need to refresh your memory).
The Java package declaration is the first line of code in any Android application class, or in any
Java class in any application for that matter. The package declaration tells Java how to package
your application. Recall the first line of code in our Hello Universe application’s MainActivity.java
Activity class, as shown in Figure 3-8:
package absolute.beginner.hellouniverse;
After the package keyword and declaration come the import statements, which import existing
Java classes and packages into your declared package. So, a package is not only for your own
code that you write yourself, but also for all code that your application uses, even if it is open source
platform code, or even code that has been written by another programmer or company, or, in the
case of Android applications, Android API code, which serves up Android OS functionality that is
only available within the Android OS.
Basically, a package naming strategy is similar to the folder naming hierarchy on your computer.
A package is just a way of organizing or grouping Java code according to its functionality. As an
example, Android organizes its classes into many logical packages, which we will routinely import
and use throughout this book.
Each Android API Level (Level 19 for the 32-bit Android KitKat OS version 4.4, or Level 20 for the
64-bit Android 5 OS) contains a vast collection of functional packages that are utilized by developers
to access the Android OS feature set. We will take a closer look at APIs in the next section of this
chapter. In our Hello Universe application in the previous chapter, you needed the following import
statement in your MainActivity.java file to be able to utilize the Bundle class:
import android.os.Bundle;