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.

8.4.3 Method Modifiers CLASSES<br />

216<br />

For example, we can declare an abstract class Point that requires its subclasses<br />

to implement toString if they are to be complete, instantiable classes:<br />

abstract class Point {<br />

int x, y;<br />

public abstract String toString();<br />

}<br />

This abstract declaration of toString overrides the non-abstract toString<br />

method of class Object. (Class Object is the implicit direct superclass of class<br />

Point.) Adding the code:<br />

class ColoredPoint extends Point {<br />

int color;<br />

public String toString() {<br />

return super.toString() + ": color " + color; // error<br />

}<br />

}<br />

results in a compile-time error because the invocation super.toString() refers<br />

to method toString in class Point, which is abstract and therefore cannot be<br />

invoked. Method toString of class Object can be made available to class<br />

ColoredPoint only if class Point explicitly makes it available through some<br />

other method, as in:<br />

abstract class Point {<br />

int x, y;<br />

public abstract String toString();<br />

protected String objString() { return super.toString(); }<br />

}<br />

class ColoredPoint extends Point {<br />

int color;<br />

public String toString() {<br />

return objString() + ": color " + color; // correct<br />

}<br />

}<br />

8.4.3.2 static Methods<br />

DRAFT<br />

A method that is declared static is called a class method. A class method is<br />

always invoked without reference to a particular object. An attempt to reference<br />

the current object using the keyword this or the keyword super or to reference<br />

the type parameters of any surrounding declaration in the body of a class method<br />

results in a compile-time error. It is a compile-time error for a static method to<br />

be declared abstract.<br />

A method that is not declared static is called an instance method, and sometimes<br />

called a non-static method. An instance method is always invoked with

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

Saved successfully!

Ooh no, something went wrong!