Animal dog = new Dog( );
dog
- is an instance of Dog object; this is a Dog
dog
- variable declared as Animal
, therefore it provides methods that declared in the interface Animal
(Dog
object can implement multiple interfaces)
E.g this example;
public interface Animal {
public String name();
}
public interface SpecialAgent {
public int agentId();
}
public class Dog implements Animal, SpecialAgent {
public String name() {
return "Laika";
}
public int agentId() {
return 666;
}
}
Demo:
Animal animal = new Dog();
animal.name(); // Laika
animal.agentId(); // not visible for Animal interface
SpecialAgent specialAgent = (SpecialAgent)animal;
specialAgent.agentId(); // 666
apecialAgent.name(); // not visible for SpecialAgent interface;
Dog dog = (Dog)animal;
animal.name(); // Laika
specialAgent.agentId(); // 666