A reference variable can be declared as a class type or an interface type.If the variable is declared as an interface type, it can reference any object of any class that impleme
The displayName()
method is displayed as undefined because objParent
declared as type Printable
and the interface does not have such method. To be able to use method displayName()
, you can declare it in interface Printable
:
interface Printable {
void sysout();
void displayName();
}
Or cast objParent
to type Parent
first before calling method displayName()
:
Printable objParent = new Parent();
objParent = (Parent) objParent;
objParent.displayName();