next up previous
Next: 1.4 Java Class Definitions Up: 1.3 Java Data Types Previous: Conversions Between Types

1.3.2 Object Types

Object values are created by instantiating classes, which may either be built-in or program-defined. Classes are organized in a strict hierarchy. Every class C except the special built-in class Object has a unique parent in the hierarchy called the superclass of C. A descendant in the class hierarchy is called a subclass. Each subclass of a class C includes all of the members (fields and methods) of C and possibly additional members. For this reason, we say that each immediate subclass of (child of) C extends C. Note that subclass relation is transitive and reflexive. If class A is a subclass of class B and class B is a subclass of class C then A is a subclass of C (transitivity). In addition, every class A is a subclass of itself (reflexivity).

We will reserve the term extends to describe the immediate subclass (child) relation: A extends B iff A is an immediate subclass of B. Hence, the extends relation is neither transitive nor reflexive. The Object class is the top class in the hierarchy and serves as the default superclass for program-defined classes. Hence, all classes are subclasses of Object.

For example, the built-in classes Integer and Float extend the built-in class Number which extends Object. Hence, the superclass of Integer is Number, the superclass of Float is Number, and the superclass of Number is Object.

Object values are actually references to objects. For this reason, two different fields can be bound to exactly the same object. In Java, objects are never implicitly copied. When a field or method parameter v is bound to an object o, the value of v is a reference to the object o, not a copy of o! (Scheme follows exactly the same conventions with regard to copying program data.)

Every Java class C has an associated type C consisting of all instances of class C and all of its subclasses. Hence, the type Object contains all object values. The built-in class String has the class Object as its superclass. Since the class String has no subclasses, the only values of type String are instances of the class String. In contrast, the built-in class Number is a child of class Object and has several subclasses including Integer and Float. Hence, all instances of the classes Integer and Float are values of type Number.

In Java, every field and method has a declared type given as part of its definition. For a method, the declared type includes the type of the result and the types of the parameters. In the Motto class above, the main method has result type void, which is empty. In Java, there are no values of type void. A method with void return type does not return a value.

Java determines the type of every program expression using a simple set of rules and confirms that

We will discuss these ``type-checking'' rules in more detail in Section 1.11.2.

next up previous
Next: 1.4 Java Class Definitions Up: 1.3 Java Data Types Previous: Conversions Between Types
Corky Cartwright