go to previous page   go to home page   go to next page highlighting

Answer:

A private method can only be used by other methods of a class. But an interface is not a class and there are no methods that use other methods.

The methods cannot be protected for a similar reason.


Example Interface

If a class needs to implement several interfaces, do this:

class SomeClass extends SomeParent implements interfaceA, interfaceB, interfaceC
{

}

Here is an example interface definition. The constants don't have to be separated from the methods (as here), but doing so makes the interface easier to read.


interface MyInterface
{
  public static final int aConstant = 32;     // a constant
  public static final double pi = 3.14159;    // a constant

  public void methodA( int x );               // a method header
  public double methodB();                    // a method header
}

A method in an interface cannot be made private. A method in an interface is public by default. The constants in an interface are public static final by default. Recall that final means that the value cannot change as the program is running.

Making use of the defaults, the above interface is equivalent to the following:


interface MyInterface
{
  int aConstant = 32;     // a constant (public static final, by default)
  double pi = 3.14159;    // a constant (public static final, by default)

  void methodA( int x );  // a method header (public, by default)
  double methodB();       // a method header (public, by default)
}

The second interface (above) is the preferred way to define an interface. The defaults are assumed and not explicitly coded.


QUESTION 4:

interface SomeInterface
{
  public final int x = 32;
  public double y;

  public double addup( );
}

Inspect the interface. Is it correct? Click here for a



go to previous page   go to home page   go to next page