From The Java Tutorials:
In Java, a class can inherit from only one class but it can implement
more than one interface. Therefore, objects can have multiple types:
the type of their own class and the types of all the interfaces that
they implement. This means that if a variable is declared to be the
type of an interface, its value can reference any object that is
instantiated from any class that implements the interface.
Can anyone provide me a basic pseudo type for this. I did not understand the bold lines.
Let’s declare two interfaces and a class that implements them both:
In the following code, it can be seen that a
Cinstance has the type ofCas well asI1andI2:Now let’s declare a class
Bwhich implementsI1as well:If we declare a variable of type
I1, we can set it to an instance ofC, and then reassign it to an instance ofB:We can also reassign it to an instance of
D, whereDextendsC: