Creating object with reference to Interface

2020-05-29 10:53发布

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 implements the interface.

Based on the above statement I have made a code on understanding. As said above declared as an interface type, it can reference any object of any class that implements the interface.

But in my code is displaying displayName()method undefined at objParent.displayName();.

public class OverridenClass
{   
    public static void main(String[] args) 
    {
     Pritable objParent = new Parent();
     objParent.sysout();
     objParent.displayName();    
    }
}

interface Pritable
{
    void sysout();
}

class Parent implements Pritable
{
    public void displayName() 
    {
     System.out.println("This is Parent Name");
    }

    public void sysout()
    {
        System.out.println("I am Printable Interfacein Parent Class");
    }
}

I am sure I have understood the wrong way.

Can some one explains the same.

Thanks for Reply.

标签: java
6条回答
孤傲高冷的网名
2楼-- · 2020-05-29 11:21

But in my code is displaying displayName()method undefined.

Right, because displayName is not defined in the Pritable 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(Pritable 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 Pritable 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.

查看更多
Ridiculous、
3楼-- · 2020-05-29 11:21

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.

查看更多
狗以群分
4楼-- · 2020-05-29 11:22

You need to type cast it to get the access to the Parent methods

((Parent)objParent).displayName();
查看更多
冷血范
5楼-- · 2020-05-29 11:22

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.

查看更多
在下西门庆
6楼-- · 2020-05-29 11:36

The displayName() method is displayed as undefined because objParent declared as type Pritable and the interface does not have such method. To be able to use method displayName(), you can declare it in interface Pritable:

interface Pritable
{
    void sysout();
    void displayName();
}

Or cast objParent to type Parent first before calling method displayName():

Pritable objParent = new Parent();
objParent = (Parent)objParent;
objParent.displayName();
查看更多
劫难
7楼-- · 2020-05-29 11:36

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.

查看更多
登录 后发表回答