Java Basics  «Prev  Next»

Lesson 3 Java's advanced object-oriented features
ObjectiveStudy interfaces, packages, access control, and working with Java's garbage collector.
Question: What is the difference between a package and interface in Java?
Answer: All Java classes are part of a package. A Java class can be explicitly defined in a named package; otherwise, it becomes part of a default package, which does not have a name. A package statement is used to explicitly define which package a class is in. If a class includes a package statement, it must be the first statement in the class definition:
package certification;
class Course {
}

Java has a well-defined structure and hierarchy. The organization's structure and components can be compared with Java’s class structure and components, and the organization's departments can be compared with Java packages. Restricting access to some data in the organization can be compared to Java's access modifiers. An organization's special privileges can be compared to nonaccess modifiers in Java.
Figure 1.3: Components of a Java class

Interfaces, Packages, and Access Control

This module discusses interfaces, packages, access control, and working with Java's garbage collector.
The series of lessons in this module will help you:
  1. Implement an interface
  2. Identify when a method can and cannot access members in other classes based on their keywords
  3. Use keywords to apply access control to variables and methods
  4. Describe the role that packages play in access control

If you already know some of these topics, you should feel free to jump around to those areas you are most interested in. While some of the exercises and student projects build on those in previous modules, we will always provide a starting point so that each module can be worked on independently from what came before it.
Packages will be introduced in the next section.

Classes and interfaces in the same package can use each other without prefixing their names with the package name. But to use a class or an interface from another package, you must use its fully qualified name. Because this can be tedious and can make your code difficult to read, you can use the import statement to use the simple name of a class or interface in your code When we discuss inheritance in the context of an object-oriented programming language such as Java, we talk about how a class can inherit the properties and behavior of another class. The class that inherits from another class can also define additional properties and behaviors.

Object-oriented System Development

If there is a single motivating factor behind object-oriented system development, it is the desire to make software development easier and more natural by raising the level of abstraction to the point where applications can be implemented in the same terms in which they are described by users. Indeed, the name object was chosen because "everyone knows what an object is." The real question, then, is not so much "What is an object?" but "What do objects have to do with system development?"
Let us develop the notion of an object through an example. A car is an object a real-world entity, identifiably separate from its surroundings. A car has a well-defined set of attributes in relation to other objects-such as color, manufacturer, cost, and owner-and a well-defined set of things you normally do with it-drive it, lock it, tow it, and carry passengers in it. In an object model, we call the for-mer properties or attributes and the latter procedures or methods. Properties (or attributes) describe the state (data) of an object. Methods (procedures) define its behavior. Stocks and bonds might be objects for a financial investment application.