Sunday, November 8, 2009

SCJP 1.6 Study Guide: Declarations and Access Controls Part 5

Declaring Interfaces

Part 3 and Part 4 of Declarations and Access Controls discussed how we can declare and modify classes. This part will turn our attention to declaring interfaces.

Interfaces are 100% abstract classes, they define abstract methods which creates a contract on what a class can do without imposing how the class will do it. Classes do not extend interfaces they implement them and when they implement interfaces they should implement the methods defined in the interfaces. Interfaces are declared using the interface keyword.

Example:
package bobhub.food;
public interface Edible{
void cook();
void prepare();
}
package bobhub.food;
public class Apple implements Edible{
public void cook(){
// you don't need to actually write code here
// you just need to change the semi-colon to
// a pair of braces
}

public void prepare(){
System.out.println("Slice... Slice... Slice...");
}
}
When I said earlier that interfaces are 100% abstract classes I mean you can only define abstract methods and constants in an interface, no more no less. The following are rules on declaring interfaces:
  • An interface must be declared with the interface keyword, not the class keyword.
  • An interface can extend one or more interfaces (comma-separated).
  • An interface can only extend another interface.
  • An interface cannot implement anything (A class or another interface).
  • Interfaces can have public or default access.
  • All interface methods are implicitly public and abstract. Take a look at the prepare() method of the Edible interface:
    void prepare();
    We can only define abstract methods in an interface but why is the prepare() method defined without the public and abstract keywords? Since interface methods are implicitly public and abstract we don't need to mark interface methods with those keywords. Marking them with public and abstract is legal but redundant. So we can define the prepare() method like these:
    public abstract void prepare();
    OR
    public void prepare();
    OR
    abstract void prepare()
  • Interface methods cannot be marked with these keywords: static, final, strictfp or native.
  • All variables defined in an interface should be and implicitly public, static and final, which means they should all be constants. The following are valid interface constant declarations:
    int MINIMUM_PREPARATION_TIME;
    OR
    final int MINIMUM_PREPARATION_TIME;
    OR
    static int MINIMUM_PREPARATION_TIME;
    OR
    public int MINIMUM_PREPARATION_TIME;
    OR
    static final int MINIMUM_PREPARATION_TIME;
    OR
    public final int MINIMUM_PREPARATION_TIME;
    OR
    public static int MINIMUM_PREPARATION_TIME;
    OR
    public static final int MINIMUM_PREPARATION_TIME;
When a class implements an interface it inherits all the constants from the interface. But remember that you cannot change the value of a constant. Also, we can interchange the order of the modifiers we use. The Apple class in the following code will not compile:
package bobhub.food;
public interface Edible{
int MINIMUM_PREPARATION_TIME = 60;
void prepare();
}
package bobhub.food;
public class Apple implements Edible{
public void prepare(){
MINIMUM_PREPARATION_TIME = 120;
}
}

That's it for now for interfaces but we will discuss them again when we talk about Polymorphism as we will learn that interface types can be used polymorphically. In the next post we will learn how we can add class members(instance and static variables and methods) and modify them with access modifiers.

0 comments:

Post a Comment