![]() |
|
Java Interfaces, Free online Core Java Programming Tutorial, Learn Java Programming Language online for free... |
| Lessons | Java Interfaces |
|
|
An interface is declared as shown below: public interface Employee {
void RaiseSalary( double d );
double GetSalary();
}
Note that both methods are implicitly public and abstract (those keywords can be provided, but are not necessary in an interface).
A class can implement one or more interfaces (in addition to extending one class). It must provide bodies for all of the methods declared in the interface, or else it must be abstract. For example: public class TA implements Employee {
void RaiseSalary( double d ) {
// actual code here
}
double GetSalary() {
// actual code here
}
}
Public interfaces (like public classes) must be in a file with the same name. A single class can implement many interfaces at the same time thus achieving multiple inheritance.
An interface can also be used as a "marker" with no fields or methods. A marker interface is used only to "mark" a class as having a property, and is testable via the instanceof operator.
Next >>> Lesson No. 13: Type Conversion in Java
|