Questions and Exercises: Object-Oriented Programming Concepts


Questions and Exercises: Object-Oriented Programming Concepts


Questions




  1. Real-world objects contain ___ and ___.


  2. A software object’s state is stored in ___.


  3. A software object’s behavior is exposed through ___.


  4. Hiding internal data from the outside world, and accessing it only through publicly exposed methods is known as data ___.


  5. A blueprint for a software object is called a ___.


  6. Common behavior can be defined in a ___ and inherited into a ___ using the ___ keyword.


  7. A collection of methods with no implementation is called an ___.


  8. A namespace that organizes classes and interfaces by functionality is called a ___.


  9. The term API stands for ___?

Exercises




  1. Create new classes for each real-world object that you observed at the beginning of this trail. Refer to the Bicycle class if you forget the required syntax.


  2. For each new class that you’ve created above, create an interface that defines its behavior, then require your class to implement it. Omit one or two methods and try compiling. What does the error look like?
Check your answers.

Answers to Questions and Exercises: Object-Oriented Programming Concepts

Answers to Questions and Exercises: Object-Oriented Programming Concepts


Answers to Questions



  1. Real-world objects contain state and behavior.


  2. A software object’s state is stored in fields.


  3. A software object’s behavior is exposed through methods.


  4. Hiding internal data from the outside world, and accessing it only through publicly exposed methods is known as data encapsulation.


  5. A blueprint for a software object is called a class.


  6. Common behavior can be defined in a superclass and inherited into a subclass using the extends keyword.


  7. A collection of methods with no implementation is called an interface.


  8. A namespace that organizes classes and interfaces by functionality is called a package.


  9. The term API stands for Application Programming Interface.

Answers to Exercises



  1. Your answers will vary depending on the real-world objects that you are modeling.


  2. Your answers will vary here as well, but the error message will specifically list the required methods that have not been implemented.

What Is a Package?

What Is a Package?

A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer. You might keep HTML pages in one folder, images in another, and scripts or applications in yet another. Because software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.

The Java platform provides an enormous class library (a set of packages) suitable for use in your own applications. This library is known as the “Application Programming Interface”, or “API” for short. Its packages represent the tasks most commonly associated with general-purpose programming. For example, a String object contains state and behavior for character strings; a File object allows a programmer to easily create, delete, inspect, compare, or modify a file on the filesystem; a Socket object allows for the creation and use of network sockets; various GUI objects control buttons and checkboxes and anything else related to graphical user interfaces. There are literally thousands of classes to choose from. This allows you, the programmer, to focus on the design of your particular application, rather than the infrastructure required to make it work.


The Java Platform API Specification contains the complete listing for all packages, interfaces, classes, fields, and methods supplied by the Java Platform 6, Standard Edition. Load the page in your browser and bookmark it. As a programmer, it will become your single most important piece of reference documentation.


What Is Inheritance?

What Is Inheritance?

Different kinds of objects often have a certain amount in common with each other. Mountain bikes, road bikes, and tandem bikes, for example, all share the characteristics of bicycles (current speed, current pedal cadence, current gear). Yet each also defines additional features that make them different: tandem bicycles have two seats and two sets of handlebars; road bikes have drop handlebars; some mountain bikes have an additional chain ring, giving them a lower gear ratio.

Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example, Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike. In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses:



A diagram of classes in a hierarchy.


A hierarchy of bicycle classes.


The syntax for creating a subclass is simple. At the beginning of your class declaration, use the extends keyword, followed by the name of the class to inherit from:

class MountainBike extends Bicycle {

// new fields and methods defining a mountain bike would go here

}

This gives MountainBike all the same fields and methods as Bicycle, yet allows its code to focus exclusively on the features that make it unique. This makes code for your subclasses easy to read. However, you must take care to properly document the state and behavior that each superclass defines, since that code will not appear in the source file of each subclass.

What Is a Class?


What Is a Class?

In the real world, you’ll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.

The following Bicycle class is one possible implementation of a bicycle:


class Bicycle {

int cadence = 0;
int speed = 0;
int gear = 1;

void changeCadence(int newValue) {
cadence = newValue;
}

void changeGear(int newValue) {
gear = newValue;
}

void speedUp(int increment) {
speed = speed + increment;
}

void applyBrakes(int decrement) {
speed = speed – decrement;
}

void printStates() {
System.out.println(“cadence:”+cadence+” speed:”+speed+” gear:”+gear);
}
}

The syntax of the Java programming language will look new to you, but the design of this class is based on the previous discussion of bicycle objects. The fields cadence, speed, and gear represent the object’s state, and the methods (changeCadence, changeGear, speedUp etc.) define its interaction with the outside world.

You may have noticed that the Bicycle class does not contain a main method. That’s because it’s not a complete application; it’s just the blueprint for bicycles that might be used in an application. The responsibility of creating and using new Bicycle objects belongs to some other class in your application.


Here’s a BicycleDemo class that creates two separate Bicycle objects and invokes their methods:


class BicycleDemo {
public static void main(String[] args) {

// Create two different Bicycle objects
Bicycle bike1 = new Bicycle();
Bicycle bike2 = new Bicycle();

// Invoke methods on those objects
bike1.changeCadence(50);
bike1.speedUp(10);
bike1.changeGear(2);
bike1.printStates();

bike2.changeCadence(50);
bike2.speedUp(10);
bike2.changeGear(2);
bike2.changeCadence(40);
bike2.speedUp(10);
bike2.changeGear(3);
bike2.printStates();
}
}

The output of this test prints the ending pedal cadence, speed, and gear for the two bicycles:
cadence:50 speed:10 gear:2
cadence:40 speed:20 gear:3

What Is an Object?

What Is an Object?

Objects are key to understanding object-oriented technology. Look around right now and you’ll find many examples of real-world objects: your dog, your desk, your television set, your bicycle.

Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.


Take a minute right now to observe the real-world objects that are in your immediate area. For each object that you see, ask yourself two questions: “What possible states can this object be in?” and “What possible behavior can this object perform?”. Make sure to write down your observations. As you do, you’ll notice that real-world objects vary in complexity; your desktop lamp may have only two possible states (on and off) and two possible behaviors (turn on, turn off), but your desktop radio might have additional states (on, off, current volume, current station) and behavior (turn on, turn off, increase volume, decrease volume, seek, scan, and tune). You may also notice that some objects, in turn, will also contain other objects. These real-world observations all translate into the world of object-oriented programming.





A circle with an inner circle filled with items, surrounded by gray wedges representing methods that allow access to the inner circle.


A software object.


Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Methods operate on an object’s internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object’s methods is known as data encapsulation — a fundamental principle of object-oriented programming.


Consider a bicycle, for example:





A picture of an object, with bibycle methods and instance variables.


A bicycle modeled as a software object.

By attributing state (current speed, current pedal cadence, and current gear) and providing methods for changing that state, the object remains in control of how the outside world is allowed to use it. For example, if the bicycle only has 6 gears, a method to change gears could reject any value that is less than 1 or greater than 6.

Bundling code into individual software objects provides a number of benefits, including:



  1. Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.


  2. Information-hiding: By interacting only with an object’s methods, the details of its internal implementation remain hidden from the outside world.


  3. Code re-use: If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.


  4. Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.

Creating and Using Extensions

Creating and Using Extensions

Any set of packages or classes can easily be made to play the role of an extension. The first step in turning a set of classes into an extension is to bundle them in a JAR file. Once that’s done, you can turn the software into an extension in two ways:

  • by placing the JAR file in a special location in the directory structure of the Java Runtime Environment, in which case it’s called an installed extension.

  • by referencing the JAR file in a specified way from the manifest of the another JAR file, in which case it’s called a download extension.

This lesson shows you how the extension mechanism works by using a simple “toy” extension as an example.


Installed Extensions


In this section, you’ll create a simple installed extension and see how extension software is treated as part of the platform by the runtime environment.

Download Extensions


This section will show you how modify a JAR file’s manifest so that the JAR-bundled software can make use of download extensions.

Understanding Extension Class Loading


This section is a short detour that summarizes the Java platform’s delegation model for loading classes, and shows how it relates to loading classes in extensions.

Understanding Extension Class Loading

Understanding Extension Class Loading

The extension framework makes use of the class-loading delegation mechanism. When the runtime environment needs to load a new class for an application, it looks for the class in the following locations, in order:

  1. Bootstrap classes: the runtime classes in rt.jar, internationalization classes in i18n.jar, and others.

  2. Installed extensions: classes in JAR files in the lib/ext directory of the JRE, and in the system-wide, platform-specific extension directory (such as /usr/jdk/packages/lib/ext on the SolarisTM Operating System, but note that use of this directory applies only to JavaTM 6 and later).

  3. The class path: classes, including classes in JAR files, on paths specified by the system property java.class.path. If a JAR file on the class path has a manifest with the Class-Path attribute, JAR files specified by the Class-Path attribute will be searched also. By default, the java.class.path property’s value is ., the current directory. You can change the value by using the -classpath or -cp command-line options, or setting the CLASSPATH environment variable. The command-line options override the setting of the CLASSPATH environment variable.

The precedence list tells you, for example, that the class path is searched only if a class to be loaded hasn’t been found among the classes in rt.jar, i18n.jar or the installed extensions.


Unless your software instantiates its own class loaders for special purposes, you don’t really need to know much more than to keep this precedence list in mind. In particular, you should be aware of any class name conflicts that might be present. For example, if you list a class on the class path, you’ll get unexpected results if the runtime environment instead loads another class of the same name that it found in an installed extension.


The Java Class Loading Mechanism


The Java platform uses a delegation model for loading classes. The basic idea is that every class loader has a “parent” class loader. When loading a class, a class loader first “delegates” the search for the class to its parent class loader before attempting to find the class itself.

Here are some highlights of the class-loading API:



  • Constructors in java.lang.ClassLoader and its subclasses allow you to specify a parent when you instantiate a new class loader. If you don’t explicitly specify a parent, the virtual machine’s system class loader will be assigned as the default parent.

  • The loadClass method in ClassLoader performs these tasks, in order, when called to load a class:

    1. If a class has already been loaded, it returns it.

    2. Otherwise, it delegates the search for the new class to the parent class loader.

    3. If the parent class loader does not find the class, loadClass calls the method findClass to find and load the class.

  • The findClass method of ClassLoader searches for the class in the current class loader if the class wasn’t found by the parent class loader. You will probably want to override this method when you instantiate a class loader subclass in your application.

  • The class java.net.URLClassLoader serves as the basic class loader for extensions and other JAR files, overriding the findClass method of java.lang.ClassLoader to search one or more specified URLs for classes and resources.

To see a sample application that uses some of the API as it relates to JAR files, see the Using JAR-related APIs lesson in this tutorial.


Class Loading and the java Command


The Java platform’s class-loading mechanism is reflected in the java command.

  • In the java tool, the -classpath option is a shorthand way to set the java.class.path property.

  • The -cp and -classpath options are equivalent.

  • The -jar option runs applications that are packaged in JAR files. For a description and examples of this option, see the Running JAR-Packaged Software lesson in this tutorial.

Download Extensions


Download Extensions

Download extensions are sets of classes (and related resources) in JAR files. A JAR file’s manifest can contain headers that refer to one or more download extensions. The extensions can be referenced in one of two ways:

  • by a Class-Path header

  • by an Extension-List header
Note that at most one of each is allowed in a manifest. Download extensions indicated by a Class-Path header are downloaded only for the lifetime of the application that downloads them, such as a web browser. Their advantage is that nothing is installed on the client; their disadvantage is that they are downloaded each time they are needed. Download extensions that are downloaded by an Extension-List header are installed into the /lib/ext directory of the JRE that downloads them. Their advantage is that they are downloaded the first time they’re needed; subsequently they can be used without downloading. But as shown later in this tutorial, they are more complex to deploy.

Since download extensions that use the Class-Path headers are simpler, lets consider them first. Assume for example that a.jar and b.jar are two JAR files in the same directory, and that the manifest of a.jar contains this header:


Class-Path: b.jar
Then the classes in b.jar serve as extension classes for purposes of the classes in a.jar. The classes in a.jar can invoke classes in b.jar without b.jar‘s classes having to be named on the class path. a.jar may or may not itself be an extension. If b.jar weren’t in the same directory as a.jar, then the value of the Class-Path header should be set to the relative pathname of b.jar.

There’s nothing special about the classes that are playing the role of a download extension. They are treated as extensions solely because they’re referenced by the manifest of some other JAR file.


To get a better understanding of how download extensions work, let’s create one and put it to use.


An Example


Suppose you want to create an applet that makes use of the RectangleArea class of the previous section:
public final class RectangleArea {
public static int area(java.awt.Rectangle r) {
return r.width * r.height;
}
}
In the previous section, you made the RectangleArea class into an installed extension by placing the JAR file containing it into the lib/ext directory of the JRE. By making it an installed extension, you enabled any application to use the RectangleArea class as if it were part of the Java platform.

If you want to be able to use the RectangleArea class from an applet, the situation is a little different. Suppose, for example, that you have an applet, AreaApplet, that makes use of class RectangleArea:


import java.applet.Applet;
import java.awt.*;

public class AreaApplet extends Applet {
Rectangle r;

public void init() {
int width = 10;
int height = 5;

r = new Rectangle(width, height);
}

public void paint(Graphics g) {
g.drawString(“The rectangle’s area is ”
+ RectangleArea.area(r), 10, 10);
}
}

This applet instantiates a 10 x 5 rectangle and then displays the rectangle’s area by using the RectangleArea.area method.

However, you can’t assume that everyone who downloads and uses your applet is going to have the RectangleArea class available on their system, as an installed extension or otherwise. One way around that problem is to make the RectangleArea class available from the server side, and you can do that by using it as a download extension.


To see how that’s done, let’s assume that you’ve bundled AreaApplet in a JAR file called AreaApplet.jar and that the class RectangleArea is bundled in RectangleArea.jar. In order for RectangleArea.jar to be treated as a download extension, RectangleArea.jar must be listed in the Class-Path header in AreaApplet.jar‘s manifest. AreaApplet.jar‘s manifest might look like this, for example:


Manifest-Version: 1.0
Class-Path: RectangleArea.jar
The value of the Class-Path header in this manifest is RectangleArea.jar with no path specified, indicating that RectangleArea.jar is located in the same directory as the applet’s JAR file.

More about the Class-Path Header


If an applet or application uses more than one extension, you can list multiple URLs in a manifest. For example, the following is a valid header:
Class-Path: area.jar servlet.jar images/
In the Class-Path header any URLs listed that don’t end with ‘/‘ are assumed to be JAR files. URLs ending in ‘/‘ indicate directories. In the preceding example, images/ might be a directory containing resources needed by the applet or the application.

Note that only one Class-Path header is allowed in a manifest file, and that each line in a manifest must be no more than 72 characters long. If you need to specify more class path entries than will fit on one line, you can extend them onto subsequent continuation lines. Begin each continuation line with two spaces. For example:


Class-Path: area.jar servlet.jar monitor.jar datasource.jar
provider.jar gui.jar
A future release may remove the limitation of having only one instance of each header, and of limiting lines to only 72 characters.

Download extensions can be “daisy chained”, meaning that the manifest of one download extension can have a Class-Path header that refers to a second extension, which can refer to a third extension, and so on.


Installing Download Extensions


In the above example, the extension downloaded by the applet is available only while the browser which loaded the applet is still running. However, applets can trigger installation of extensions, if additional information is included in the manifests of both the applet and the extension.

Since this mechanism extends the platform’s core API, its use should be judiciously applied. It is rarely appropriate for interfaces used by a single, or small set of applications. All visible symbols should follow reverse domain name and class hierarchy conventions.


The basic requirements are that both the applet and the extensions it uses provide version information in their manifests, and that they be signed. The version information allows Java Plug-in to ensure that the extension code has the version expected by the applet. For example, the AreaApplet could specify an areatest extension in its manifest:


Manifest-Version: 1.0
Extension-List: areatest
areatest-Extension-Name: area
areatest-Specification-Version: 1.1
areatest-Implementation-Version: 1.1.2
areatest-Implementation-Vendor-Id: com.sun
areatest-Implementation-URL: http://www.sun.com/test/area.jar
(Please note that there is no area.jar file at the above URL; this is just an example!) The manifest in area.jar would provide corresponding information:
Manifest-Version: 1.0
Extension-Name: area
Specification-Vendor: Sun Microsystems, Inc
Specification-Version: 1.1
Implementation-Vendor-Id: com.sun
Implementation-Vendor: Sun Microsystems, Inc
Implementation-Version: 1.1.2
Both the applet and the extension must be signed, by the same signer. Signing the jar files will modify them in-place, providing more information in their manifest files. Signing helps ensure that only trusted code gets installed. A simple way to sign jar files is to first create a keystore, and then use that to hold certificates for the applet and extension. For example:
keytool -genkey -dname “cn=Fred” -alias test -keypass mykeypass -storepass mystorepass -validity 180
Then the jar files can be signed:
jarsigner -storepass mystorepass -keypass mykeypass AreaApplet.jar test
jarsigner -storepass mystorepass -keypass mykeypass area.jar test
More information on keytool, jarsigner, and other security tools is at the Summary of Tools for the Java 2 Platform Security.

Here is AreaDemo.html, which loads the applet and causes the extension code to be downloaded and installed:


<html>
<body>
<applet code=”AreaApplet.class” archive=”AreaApplet.jar”/>
</body>
</html>
When the page is loaded for the first time, the user is told that the applet requires installation of the extension. A subsequent dialog informs the user about the signed applet. Accepting both installs the extension in the lib/ext folder of the JRE and runs the applet.

After restarting the web browser and load the same web page, only the dialog about the applet’s signer is presented, because area.jar is already installed. This is also true if AreaDemo.html is opened in a different web browser (assuming both browsers use the same JRE).

Installed Extensions

Installed Extensions

Installed extensions are JAR files in the lib/ext directory of the Java Runtime Environment (JRETM) software. As its name implies, the JRE is the runtime portion of the Java Development Kit containing the platform’s core API but without development tools such as compilers and debuggers. The JRE is available either by itself or as part of the Java Development Kit.

As of the Java 1.2 platform, the JRE is a strict subset of the JDKTM software. A subset of the JDK software directory tree looks like this:





The JRE consists of those directories within the highlighted box in the diagram. Whether your JRE is stand-alone or part of the JDK software, any JAR file in the lib/ext of the JRE directory is automatically treated by the runtime environment as an extension.


Since installed extensions extend the platform’s core API, use them judiciously. They are rarely appropriate for interfaces used by a single, or small set of applications.


Furthermore, since the symbols defined by installed extensions will be visible in all Java processes, care should be taken to ensure that all visible symbols follow the appropriate “reverse domain name” and “class hierarchy” conventions. For example, com.mycompany.MyClass.


As of Java 6, extension JAR files may also be placed in a location that is independent of any particular JRE, so that extensions can be shared by all JREs that are installed on a system. Prior to Java 6, the value of java.ext.dirs referred to a single directory, but as of Java 6 it is a list of directories (like CLASSPATH) that specifies the locations in which extensions are searched for. The first element of the path is always the lib/ext directory of the JRE. The second element is a directory outside of the JRE. This other location allows extension JAR files to be installed once and used by several JREs installed on that system. The location varies depending on the operating system:



  • SolarisTM Operating System: /usr/jdk/packages/lib/ext

  • Linux: /usr/java/packages/lib/ext

  • Microsoft Windows: %SystemRoot%\Sun\Java\lib\ext

Note that an installed extension placed in one of the above directories extends the platform of each of the JREs (Java 6 or later) on that system.



A Simple Example


Let’s create a simple installed extension. Our extension consists of one class, RectangleArea, that computes the areas of rectangles:
public final class RectangleArea {
public static int area(java.awt.Rectangle r) {
return r.width * r.height;
}
}
This class has a single method, area, that takes an instance of java.awt.Rectangle and returns the rectangle’s area.

Suppose that you want to test RectangleArea with an application called AreaApp:


import java.awt.*;

public class AreaApp {
public static void main(String[] args) {
int width = 10;
int height = 5;

Rectangle r = new Rectangle(width, height);
System.out.println(“The rectangle’s area is ”
+ RectangleArea.area(r));
}
}

This application instantiates a 10 x 5 rectangle, and then prints out the rectangle’s area using the RectangleArea.area method.


Running AreaApp Without the Extension Mechanism

Let’s first review how you would run the AreaApp application without using the extension mechanism. We’ll assume that the RectangleArea class is bundled in a JAR file named area.jar.

The RectangleArea class is not part of the Java platform, of course, so you would need to place the area.jar file on the class path in order to run AreaApp without getting a runtime exception. If area.jar was in the directory /home/user, for example, you could use this command:


java -classpath .:/home/user/area.jar AreaApp
The class path specified in this command contains both the current directory, containing AreaApp.class, and the path to the JAR file containing the RectangleArea package. You would get the desired output by running this command:
The rectangle’s area is 50


Running AreaApp by Using the Extension Mechanism

Now let’s look at how you would run AreaApp by using the RectangleArea class as an extension.

To make the RectangleArea class into an extension, you place the file area.jar in the lib/ext directory of the JRE. Doing so automatically gives the RectangleArea the status of being an installed extension.


With area.jar installed as an extension, you can run AreaApp without needing to specify the class path:


java AreaApp

Because you’re using area.jar as an installed extension, the runtime environment will be able to find and to load the RectangleArea class even though you haven’t specified it on the class path. Similarly, any applet or application being run by any user on your system would be able to find and use the RectangleArea class.


If there are multiple JREs (Java 6 or later) installed on a system and want the RectangleArea class to be available as an extension to all of them, instead of installing it in the lib/ext directory of a particular JRE, install it in the system-wide location. For example, on system running Linux, install area.jar in the directory /usr/java/packages/lib/ext. Then AreaApp can run using different JREs that are installed on that system, for example if different browsers are configured to use different JREs.