Implementing an Interface

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

Answer:

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

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 of the class definition is the same as always. However, since it implements an interface the body must have a definition of 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. Here is another class definition:

public class SmallClass implements InterfaceA
{

   class definition body

}

Any number of classes can implement the same interfaces.

QUESTION 5:

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