The exercise requires you to perform three tasks:
- Define an interface called
Printable *
- Write a static method that takes
Printable[] as a parameter
- Write an otherwise empty class that implements
Printable
- In your
main method, create an array of Printable, and fill it with instances of the class defined in step 3
- Pass the array defined in step 4 to the static method defined in step 2 to check that your code works correctly.
Here is a suggestion for an interface:
public interface Printable {
void print();
}
Here is a suggestion for the class implementing Printable:
public class TestPrintable implements Printable {
public void print() {
System.out.println("Hello");
}
}
The static method should have a signature that looks like this:
public static void printAll(Printable[] data) {
... // Write your implementation here
}
The test can looks like this:
public void main(String[] args) {
Printable[] data = new Printable[] {
new TestPrintable()
, new TestPrintable()
, new TestPrintable()
};
printAll(data);
}
* Java defines an interface called Printable, but it serves a different purpose.
Printableis a class that is declared as implementingPrintable. The method parameter will be an array of that type.void method(Printable[] objects) { ... }