10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

4.12.6 Types, Classes, and Interfaces TYPES, VALUES, AND VARIABLES<br />

74<br />

byte r, g, b;<br />

public void setColor(byte rv, byte gv, byte bv) {<br />

r = rv; g = gv; b = bv;<br />

}<br />

}<br />

class Test {<br />

public static void main(String[] args) {<br />

Point p = new Point();<br />

ColoredPoint cp = new ColoredPoint();<br />

p = cp;<br />

Colorable c = cp;<br />

}<br />

}<br />

In this example:<br />

• <strong>The</strong> local variable p of the method main of class Test has type Point and is<br />

initially assigned a reference to a new instance of class Point.<br />

• <strong>The</strong> local variable cp similarly has as its type ColoredPoint, and is initially<br />

assigned a reference to a new instance of class ColoredPoint.<br />

• <strong>The</strong> assignment of the value of cp to the variable p causes p to hold a reference<br />

to a ColoredPoint object. This is permitted because ColoredPoint is a<br />

subclass of Point, so the class ColoredPoint is assignment compatible<br />

(§5.2) with the type Point. AColoredPoint object includes support for all<br />

the methods of a Point. In addition to its particular fields r, g, and b, it has<br />

the fields of class Point, namely x and y.<br />

• <strong>The</strong> local variable c has as its type the interface type Colorable, so it can<br />

hold a reference to any object whose class implements Colorable; specifically,<br />

it can hold a reference to a ColoredPoint.<br />

DISCUSSION<br />

DRAFT<br />

Note that an expression such as new Colorable() is not valid because it is not possible to<br />

create an instance of an interface, only of a class.<br />

Every array also has a class; the method getClass, when invoked for an array<br />

object, will return a class object (of class Class) that represents the class of the<br />

array.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!