As I proceed through my online tutorial, I came across this lesson. I have an interface and two classes that implements that interface.
public interface Payable {
double getPaymentAmount();
}
and
- class
Invoice
that implements the above interfacePayable
- class
SalariedEmployee
that extends an abstract classEmployee
which implementsPayable
interface. - A test class that contains the main method to test this.
Now in the test class, when creating an array of objects, the type of object was referred to as Payable[]
rather than SalariedEmployee[]
or Invoice[]
, like
public static void main(String[] args) {
Payable[] payableObjects = new Payable[4];
payableObjects[0] = new Invoice("0000", "abc", 1,2);
payableObjects[1] = new SalariedEmployee("test", "user", "000-000", 35);
- Is this because all the classes implements the interface Payable[] ?
- If an interface is defined at the top level hierarchy, is it always possible to create objects of all the classes that implements that interface?