Java Reference
In-Depth Information
Self-Test Exercises (continued)
7. Suppose you have a class Circle that represents circles which all have centers at
the same point. (To make it concrete, you can take the circles to be in the usual
x , y plain and to all have their centers at the origin.) Suppose there is a boolean
valued method inside of the class Circle such that, for circles c1 and c2 ,
c1.inside(c2)
returns true if c1 is completely inside of c2 (and c2 is not the same as c1 ). Is
the following a total ordering?
c1 comes before c2 if c1 is inside of c2
(that is, if c1.inside(c2) returns true ).
You could represent objects of the class Circle by a single value of type double
that gives the radius of the circle, but the answer does not depend on such details.
Defined Constants in Interfaces
The designers of Java often used the interface mechanism to take care of a number of
miscellaneous details that do not really fit the spirit of what an interface is supposed
to be. One example of this is the use of an interface as a way to name a group of
defined constants.
An interface can contain defined constants as well as method headings, or instead of
method headings. When a method implements the interface, it automatically gets the
defined constants. For example, the following interface defines constants for months:
public interface MonthNumbers
{
public static final int JANUARY = 1,
FEBRUARY = 2, MARCH = 3, APRIL = 4, MAY = 5,
JUNE = 6, JULY = 7, AUGUST = 8, SEPTEMBER = 9,
OCTOBER = 10, NOVEMBER = 11, DECEMBER = 12;
}
Any class that implements the MonthNumbers interface will automatically have the
12 constants defined in the MonthNumbers interface. For example, consider the
following toy class:
public class DemoMonthNumbers implements MonthNumbers
{
public static void main(String[] args)
{
System.out.println(
"The number for January is " + JANUARY);
}
}
 
Search WWH ::




Custom Search