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( );
}
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 extends Parent
must be before implements SomeInterface
.
If a class extends Object
it does not need an extends
clause.
The class body is the same as with all class definitions. However, since since this class implements an interface, the class body must define or inherit each of the methods declared in the interface. 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 or inherit 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. All they have to do is implement a body that matches the method header in the interface.
public class SmallClass implements InterfaceA { class definition body }
Is the above class definition correct? What parent class does it extend?