Interfaces
Access Modifiers
Access modifiers: package-default, public
Non-access modifiers: abstract (implicitly abstract, cannot be static/final)
- Can ONLY extend multiple interfaces (not classes).
- Cannot implement interfaces or classes.
Constructors
Access modifiers: package-default, public
Non-access modifiers: abstract (implicitly abstract, cannot be static/final)
- No return type allowed.
Blocks
Cannot define initializer blocks.
Members
Access modifiers: public
Non-access modifiers: static, final (implicitly public static final)
- All members are public static final and cannot be anything else therefore it is not necessary to set the modifiers: public static final int I = 0; is the same as I = 0;
- Can only declare constants and not instance variables;
- Must be initialised.
Methods
Access modifiers: public
Non-access modifiers: abstract
- All methods are implicitly public and abstract, therefore it is not obligatory to set these key words;
- Methods have no body e.g. public void abstract method();
- Methods must be implemented/overwritten if the subclass is not an interface or an abstract class.
static/final:
- It does not make sense that abstract methods are static/final as static/final methods cannot be overridden. The purpose of abstract classes is that they are implemented/overwritten.
private:
- It does not make sense that interface methods are private as private methods cannot be inherited. The purpose of interfaces is that they are implemented/overwritten.
Behaviours
- Interfaces cannot be instantiated.
- Can be subclassed (extends) but only by interfaces/abstract classes;
- Can be implemented (implements) but only by classes;
- Only concrete classes must implement all methods i.e. non abstract classes;
- Abstract classes that implement an interface are not required to implement the methods;
- Interfaces are implemented by classes and extended by interfaces.
Leave a Reply