HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML

Java classes should be designed to enhance their reusability and flexibility. Coding to an object type rather than an implementation by using interfaces or abstract classes can help us achieve both flexibility and reusability.

What Is an Abstract Class?
Everything in Java is an object, so to write Java programs we need to define classes. A Java class in turn defines the type and the available methods for that type. It also provides the implementation of the methods that it defines. This kind of class is called a concrete class. An abstract class, defined in section of the Java Language Specification (JLS), defines the type and the available methods but is not required to implement any of the methods that it defines.

The following code shows a simple abstract class named Base.

public abstract class Base {
public abstract String f1();
public String f2(String s) {
return "Base.fs("+s+")";

An abstract class is denoted by the keyword abstract in the class declaration and usually contains one or more abstract methods. An abstract method declaration defines the method name, number and type of parameters, return type, and throws clause, but does not have an implementation. In the declaration an abstract method is denoted by the abstract keyword, as is the case with method f1 in the class Base defined earlier.

Why design with an abstract class instead of a concrete class? Using an abstract class allows you to define the interface of a type and still provide method implementations without having to implement the entire set of methods. The term interface is used here to mean the publicly accessible functions on the class, not the Java interface, which we'll look at next. The abstract class represents a type at a very generic level. Although operations can be defined for the type, the abstract class can't provide all the functionality without becoming too specific.

An abstract class is incomplete and relies on its subclasses to complete the implementation. In the following code sample the class X inherits from the class Base by extending it. The class Base is considered the superclass of class X. The class X is considered the subclass (or subtype) of class Base. For X to avoid being declared abstract, it must implement the abstract method f1 defined in the class Base. The definition of class X is listed below:

public class X extends Base{

public String f1() {
return "X.f1()";

public String f3() {
return "X.f3()";

Some important points to remember about abstract classes:

  • An abstract class cannot be instantiated.
  • An abstract class can implement zero, one, or more methods.
  • A class with an abstract method must be declared abstract.
  • A class can be declared abstract even if it has no abstract methods.
  • Methods that are static, private, and/or final cannot be abstract.
What Is an Interface?
A Java interface defines a type and the available methods for that type. It does not, and, in fact, cannot provide an implementation for any of the methods it defines. An interface is defined in section 9 of the Java Language Specification.

The following is a simple interface, iBasicType:

public interface iBasicType {
public String f1();
public String f4();

The interface and all the methods on it are automatically considered abstract and do not require the abstract keyword.

To be of any use a Java interface must be implemented by a Java class. In the following code sample, the class Basic implements the interface iBasicType. The class Basic must provide an implementation for every method in the interface, otherwise it must be declared an abstract class.

public class Basic implements iBasicType {

public String f1() {
return "Basic.f1()";

public String f4() {
return "Basic.f4()";

public String f2(String s) {
return "Basic.fs("+s+")";

A class implementing an interface can also declare additional methods that aren't part of the interface. This is shown in the Basic class definition by defining method f2, which isn't part of the iBasicType interface.

Some important points to remember:

  • An interface cannot be instantiated.
  • An interface has no implementation.
  • All the methods on an interface are considered public and abstract.
  • All the fields on an interface are considered public, static, and final.
  • An interface can extend other interfaces.
  • A class can implement one or more interfaces.
The Power of Dealing with Types
Java is a strongly typed language, so every object in Java has a type. The type associated with any particular object is its class name. For example, when the class Customer is instantiated, the object is said to be of the type Customer. Each instance of an object may have a different state, but all objects of the same type have the same set of methods because they're created from the same class. Abstract classes and interfaces give us more flexibility in our programs because they allow us to look at an object as a different type instead of just using its class name.

For example, the object Y, shown in Figure 1, can be created from the following line of code:

Figure 1

Y myYObj = new Y();

The variable myYObj contains an object reference of type Y that points to a Y object in memory after this code is executed. Through the Y object reference all the methods defined on the Y object and any class it extends can be invoked. Since Y extends the class X, they're related and we can treat the subclass Y as if it were actually any one of its superclasses, in this case X or Base; e.g., the following code is completely valid:

X myXObj = myYObj;

The variable myXObj contains an object reference of type X, but the reference points to a Y object in memory after this line of code is executed. This allows us to look at the Y object as if it were really of type X. The compiler can safely place references to subclasses into a variable that's declared to be a superclass of that subclass, since the subclass is guaranteed to contain all the methods and variables that can be called on the superclass. Simply, Y extends X, so the compiler is assured that any method that can be called on X can also be called on Y. However, the reverse is not guaranteed to be true so the following code is not valid:

Y anotherYObj = new X();
// This is not valid

Dealing with a subclass object as if it were really one of its superclasses is called upcasting or narrowing. It's important to understand that the object reference, which points to an object in memory, does not determine the type of the object that gets the request to invoke a method, nor does it change the type of the object that was instantiated. The object reference does, however, affect how the object it points to can be operated on. The object reference defines the type through which the calling program sees the object in memory. You can think of the X object reference as a filter that allows only calls to methods that are defined in the X class and any class it extends. Looking at the class diagram in Figure 1, we can call the f1, f2, and f3 methods through the X object reference. But we couldn't call the f4 method even though this is a valid method on the Y object, because it's not defined by the class X or any of its superclasses.

When we call methods through the X object reference using the myXObj variable, we're actually invoking methods in the object that the reference points to in memory. In this case it's a Y object instance.

myXObj.f1(); // returns "Y.f1()"
myXObj.f3(); // returns "X.f3()"

These method calls give the results shown in the comment sections based on the following definition of the Y class:

public class Y extends X implements iBasicType {

public String f1() {
return "Y.f1()";

public String f2(String s) {
return "Y.fs("+s+")";

public String f4() {
return "Y.f4()";

Notice that the implementation of the f1 method on the Y class is run even though we're using an X object reference. In this example, the Y class does not define its own f3 method, so the implementation of the f3 method on the X class is executed.

It's possible to point an object reference to an object that is its subclass because Java performs dynamic binding. Objects communicate by sending messages to each other through references. This is basically a means of calling methods on the object. Dynamic binding allows messages to be sent to an object without knowing the exact type of the object that the message will be sent to when the code is written. When the code is executed and a method is invoked, the JVM determines which method is actually executed based on which object is being pointed to by the object reference at runtime.

The message to execute a particular method cannot be sent to just any object. Java is a strongly typed language so the compiler makes sure the assignment is type-safe. A variable has a type that never changes, so the myXObj variable will always hold an X object reference. When the variable type is a reference type (arrays, classes, and interfaces), it can point to different objects that are related via inheritance or an interface. Specifically, a reference variable can be assigned a reference to any object whose class is:

  • Of the same type as that of the reference variable declaration
  • A subtype of the type that the reference variable is declared to be Or any object that implements:
  • The same interface that the reference variable is declared to be
  • An interface that is a subtype of the interface that the reference variable is declared to be
An Example with an Interface
We can declare variables to have a type that is defined as an abstract class or an interface even though they can't be instantiated. Given the class hierarchy shown in Figure 1, a variable can be declared as follows:

iBasicType myBasicObj;

The variable, myBasicObj, holds an object reference to the interface iBasicType. We can instantiate objects that implement the interface using the code sample below:

iBasicType myBasicObj = new Basic();

The variable, myBasicObj, holds an iBasicType reference that points to a Basic object in memory. Since the class Y also implements the interface iBasicType, the following code is also valid:

iBasicType anotherBasicObj = new Y();

By implementing an interface, the object is also declaring that it is of that type. For example, the class Y can be said to be of type Y, its class name, as well as of type iBasicType, since it implements that interface.

Like the variable that holds an object reference to a class type, an object reference to an interface type defines the way the calling program sees the object in memory. So we can call the f1 and f4 methods through the iBasicType object reference. But we could not call the f2 method even though this is a valid method on both the Basic and the Y object, because it's not defined by the interface (see Figure 1).

Wrapping It Up
Hopefully, you'll begin to see the potential flexibility we can design into our applications by coding to different types. Applying this in a practical sense will be the focus of our next article, until then, happy coding.

Author Bio
Michael Barlotta is the director of technology at AEGIS.net Inc (www.AEGIS.net). [email protected]

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.