J2EEOnlineJ2EE





Classes/Objects  «Prev  Next»
Lesson 4More on classes
ObjectiveImportant points about classes, subclasses, and superclasses.

Designing classes in Java

A few questions on the certification exam will test your ability to define classes. These questions will describe relationships using "is a" and "has a." The "is a" specifies a subclass relationship while "has a" identifies a field variable. For example, consider the description, "An employee is a person that has an integer employee number, and an hourly rate." The following class defines these relationships:

public class Employee extends Person {
 int employeeNumber;
 double hourlyRate;
}



Subclasses and superclasses

The following is a collection of definitions and rules concerning subclasses and superclasses:
  1. A class X that extends another class Y is said to be a subclass[1] of Y.
    Y is said to be a superclass[2] of X.
  2. All classes are considered to be subclasses of themselves.
  3. If a class X is a subclass of Y and Y is a subclass of Z, then X is also a subclass of Z and Z is a superclass of X.
  4. A strict subclass[3] of a class X is any subclass of X except X.
  5. The direct superclass[4] of a class X is the class which it extends.
  6. The Object class of the java.lang package is the highest class in the Java class hierarchy. All classes are subclasses of Object.
[1]Subclass: A class that extends another class. All classes are a subclass of themselves.
[2]Superclass: A class that is extended by another class.
[3]Strict subclass: A subclass that is not the class itself.
[4]Direct superclass: The class that is identified in a class's extends clause. If a class is declared without an extends clause, then its direct superclass is java.lang.Object.