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
You need to type cast it to get the access to the Parent
methods
((Parent)objParent).displayName();
Interfaces are basically another way of - breaking the rules of single inheritance.
By using interfaces, a child class can, both inherit it's parents methods and be forced to implement it's interface methods. Resulting in an easy to extend and maintain inheritance tree etc.
The catch however is, when the child is referenced under the parent, you only have access to the parent methods. To access the interface methods, you will need to cast or create the child under the interface reference type.
Interfaces also allow the collection of multiple classes of different families to be collected under the interface type. To what benefit I am yet to discover.
In my opinion, it is pointless since I still cannot achieve fully blown polymorphism anyways - by just using the parent reference type and still have access to the interface implementations.
Compiler doesn't care about run-time. as far as the compiler is concerned, it checks if the reference type has a method called display in your interface type.
methods declared in your sub-class or implementing class are not part of your super class/interface. thus you cannot invoke those methods which are declared in sub-class with super class/interface reference type.
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();
wherever the method signature reside reference of that interface will not give any error. In your example your method sysout() is in interface so reference of the interface will not give any error, but for method displayName() interface reference gives an error. For that you have to use your class reference.
But in my code is displaying displayName()method undefined.
Right, because displayName
is not defined in the Printable
interface. You can only access the methods defined on the interface through a variable declared as having that interface, even if the concrete class has additional methods. That's why you can call sysout
, but not displayName
.
The reason for this is more apparent if you consider an example like this:
class Bar {
public static void foo(Printable p) {
p.sysout();
p.displayName();
}
}
class Test {
public static final void main(String[] args) {
Bar.foo(new Parent());
}
}
The code in foo
must not rely on anything other than what is featured in the Printable
interface, as we have no idea at compile-time what the concrete class may be.
The point of interfaces is to define the characteristics that are available to the code using only an interface reference, without regard to the concrete class being used.