Previous | Next | Trail Map | Writing Java Programs | Table of Contents


Objects, Classes, and Interfaces

In the lesson titled Object-Oriented Programming Concepts: A Primer(in the Writing Java Programs trail) you learned the concepts behind object-oriented programming. Now that you have an abstract understanding of object-oriented programming, it's time to get to work and put those concepts to practical use. This lesson shows you how to use and abuse the object-oriented paradigms of the Java language.

In this lesson, you will learn how to create and destroy objects, how to create and subclass classes, how to write methods, and how to create and use interfaces. This lesson covers everything from how to protect the innards of an object from other objects and overriding methods, to creating class templates using abstract classes and methods and the patriarch of the Java object hierarchy--the java.lang.Object class.

The Life Cycle of an Object

As you learned in Object-Oriented Programming Concepts: A Primer(in the Writing Java Programs trail) , an object is a software module that has state and behaviour. An object's state is contained within its member variables and its behaviour is implemented through its methods.

Typically, Java programs that you write will create many different objects from templates known as classes. These objects interact with one another by sending each other messages. The result of a message is a method invocation which performs some action or modifies the state of the receiving object. Through these object interactions, your Java program can implement a graphical user interface, run animations, or send and receive information over the network. Once an object has completed the work for which it was created, it is destroyed and its resources recycled for the use of other objects.

The following pages describe the typical life cycle of an object: 1) creation, 2) use, and 3) destruction (through garbage collection).

Creating Your Own Classes

A Java object is an instance of a class. Frequently, we say that an object's class is the object's type. The Java development environment comes with many classes that you can use in your programs. Or you can write your own. This section shows you how to write your own class.

Declaring Member Variables

An object maintains its state through variables defined within the class. These variables are known as member variables (to differentiate them from local variables, arguments and other variables). When declaring a member variable for your class, you can specify a host of various "attributes" about that variables: its type, its name, whether other classes have access to it, and so on.

Writing Methods

An object implements its behaviour through its methods. Similar to when you declare member variables for your class, when declaring a method for your class, you can specify various "attributes" about that method: its return type, its name, its parameters, whether or not other class have access to it, and so on. In fact, many of the attributes that you declare for methods and member variables use the same Java keywords and behave in the same way (access specifiers and static, for example).

Subclasses, Superclasses, and Inheritance

The ability to derive one class from another and inherit its state and behaviour is one of object-oriented programming's most powerful paradigms. Inheritance provides a powerful and natural mechanism for organizing and structuring software programs putting the most general classes higher in the class hierarchy and the most specific classes lower in the class hierarchy. In addition, because classes inherit state and behaviour from their superclasses you don't have to write that code again--inheritance allows you to reuse code over and over again in each subclass you create.

Creating and Using Interfaces

Interfaces provide a mechanism that allow otherwise unrelated classes to implement the same set of methods. Basically, an interface is a collection of method definitions and constant values that is free from dependency on a specific class or class hierarchy.

Often, interfaces are touted as an alternative to multiple inheritance. However, multiple inheritance and interfaces actually provide very different functionality.


Previous | Next | Trail Map | Writing Java Programs | Table of Contents