CHAPTER 6: Names Previous
Previous
Java Language
Java Language
Index
Index
Next
Next

6.4 Members and Inheritance

6.4.1 The Members of a Package , 6.4.2 The Members of a Class Type , 6.4.3 The Members of an Interface Type , 6.4.4 The Members of an Array Type

Packages and reference types have members. The members of a package (Chapter 7) are subpackages (S7.1) and all the class (Chapter 8) and interface (Chapter 9) types declared in all the compilation units (S7.3) of the package. The members of a reference type (S4.3) are fields (S8.3, S9.3, S10.7) and methods (S8.4, S9.4). Members are either declared in the type, or inherited because they are accessible members of a superclass or superinterface which are neither hidden nor overridden (S8.4.6).

This section provides an overview of the members of packages and reference types here, as background for the discussion of qualified names and the determination of the meaning of names. For a complete description of membership, see S7.1, S8.2, S9.2, and S10.7.


6.4.1 The Members of a Package

A member of a package (Chapter 7) is a subpackage (S7.1), or a class (Chapter 8) or interface (Chapter 9) type declared in a compilation unit (S7.3) of the package.

In general, the subpackages of a package are determined by the host system (S7.2). However, the standard package java always includes the subpackages lang , util , io , and net and may include other subpackages. No two distinct members of the same package may have the same simple name (S7.1), but members of different packages may have the same simple name. For example, it is possible to declare a package:

package vector;
public class Vector { Object[] vec; }

that has as a member a public class named Vector , even though the standard package java.util also declares a class named Vector . These two class types are different, reflected by the fact that they have different fully qualified names (S6.7). The fully qualified name of this example Vector is vector.Vector , whereas java.util.Vector is the fully qualified name of the standard Vector class. Because the package vector contains a class named Vector , it cannot also have a subpackage named Vector .


6.4.2 The Members of a Class Type

The members of a class type (S8.2) are fields and methods. The members of a class type are all of the following:

Constructors (S8.6) are not members.

There is no restriction against a field and a method of a class type having the same simple name.

A class may have two or more fields with the same simple name if they are declared in different interfaces and inherited. An attempt to refer to any of the fields by its simple name results in a compile-time error (S6.5.6.2, S8.2).

In the example:


interface Colors {
	int WHITE = 0, BLACK = 1;
}

interface Separates {
	int CYAN = 0, MAGENTA = 1, YELLOW = 2, BLACK = 3;
}

class Test implements Colors, Separates {
	public static void main(String[] args) {
		System.out.println(BLACK); // compile-time error: ambiguous
	}
}

the name BLACK in the method main is ambiguous, because class Test has two members named BLACK , one inherited from Colors and one from Separates .

A class type may have two or more methods with the same simple name if the methods have different signatures (S8.4.2), that is, if they have different numbers of parameters or different parameter types in at least one parameter position. Such a method member name is said to be overloaded.

A class type may contain a declaration for a method with the same name and the same signature as a method that would otherwise be inherited from a superclass or superinterface. In this case, the method of the superclass or superinterface is not inherited. If the method not inherited is abstract , then the new declaration is said to implement it; if the method not inherited is not abstract , then the new declaration is said to override it.

In the example:


class Point {
	float x, y;
	void move(int dx, int dy) { x += dx; y += dy; }
	void move(float dx, float dy) { x += dx; y += dy; }
	public String toString() { return "("+x+","+y+")"; }
}

the class Point has two members that are methods with the same name, move . The overloaded move method of class Point chosen for any particular method invocation is determined at compile time by the overloading resolution procedure given in S15.11.

In this example, the members of the class Point are the float instance variables x and y declared in Point , the two declared move methods, the declared toString method, and the members that Point inherits from its implicit direct superclass Object (S4.3.2), such as the method hashCode (S20.1.4). Note that Point does not inherit the toString method (S20.1.2) of class Object because that method is overridden by the declaration of the toString method in class Point .


6.4.3 The Members of an Interface Type

The members of an interface type (S9.2) are fields and methods. The members of an interface are all of the following:

An interface may have two or more fields with the same simple name if they are declared in different interfaces and inherited. An attempt to refer to any such field by its simple name results in a compile-time error (S6.5.5.1, S9.2).

In the example:


interface Colors {
	int WHITE = 0, BLACK = 1;
}

interface Separates {
	int CYAN = 0, MAGENTA = 1, YELLOW = 2, BLACK = 3;
}
interface ColorsAndSeparates extends Colors, Separates {

	int DEFAULT = BLACK;								 	// compile-time error: ambiguous

}

the members of the interface ColorsAndSeparates include those members inherited from Colors and those inherited from Separates , namely WHITE , BLACK (first of two), CYAN , MAGENTA , YELLOW , and BLACK (second of two). The member name BLACK is ambiguous in the interface ColorsAndSeparates .


6.4.4 The Members of an Array Type

The members of an array type (S10.7) are all of the following:

The example:


class Test {
	public static void main(String[] args) {
		int[] ia = new int[3];
		int[] ib = new int[6];
		System.out.println(ia.getClass() == ib.getClass());
		System.out.println("ia has length=" + ia.length);
	}
}

produces the output:


true
ia has length=3

This example uses the method getClass inherited from class Object and the field length . The result of the comparison of the Class objects in the second println demonstrates that all arrays whose components are of type int are instances of the same array type, which is int[] .

Top© 1996 Sun Microsystems, Inc. All rights reserved.