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

Answer:

No. Variables (such as y) cannot be put in an interface. Only constants and method headers.

interface SomeInterface
{
  public final int x = 32;
  public double y;   // No variables allowed

  public double addup( );
}

Implementing an Interface

A class definition must always extend one parent, but it can implement zero or more interfaces:


class SomeClass extends Parent implements SomeInterface
{

   class definition body

}

The body is the same as with all class definitions. However, since since this class implements an interface, the class body must have a definition for each of the methods declared in the interface. The class definition can use access modifiers as usual. Here is a class definition that implements three interfaces:


public class BigClass extends Parent 
             implements  InterfaceA, InterfaceB, InterfaceC
{

   class definition body

}

Now BigClass must provide a method definition for every method declared in the three interfaces.

Any number of classes can implement the same interface. The implementations of the methods listed in the interface can be different in each class.



QUESTION 5:

public class SmallClass implements InterfaceA
{

   class definition body

}

Is the above class definition correct? What parent class does it extend?


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