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;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:
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...");
}
}
- 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
andabstract
. Take a look at theprepare()
method of theEdible
interface:void prepare();
We can only define abstract methods in an interface but why is theprepare()
method defined without thepublic
andabstract
keywords? Since interface methods are implicitlypublic
andabstract
we don't need to mark interface methods with those keywords. Marking them withpublic
andabstract
is legal but redundant. So we can define the prepare() method like these:public abstract void prepare();
ORpublic void prepare();
ORabstract void prepare()
- Interface methods cannot be marked with these keywords:
static
,final
,strictfp
ornative
. - All variables defined in an interface should be and implicitly
public
,static
andfinal
, which means they should all be constants. The following are valid interface constant declarations:int MINIMUM_PREPARATION_TIME;
ORfinal int MINIMUM_PREPARATION_TIME;
ORstatic int MINIMUM_PREPARATION_TIME;
ORpublic int MINIMUM_PREPARATION_TIME;
ORstatic final int MINIMUM_PREPARATION_TIME;
ORpublic final int MINIMUM_PREPARATION_TIME;
ORpublic static int MINIMUM_PREPARATION_TIME;
ORpublic static final int MINIMUM_PREPARATION_TIME;
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