Java Beginner
Java Beginner

Reputation: 1655

Creating object with reference to Interface

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) {
        Printable objParent = new Parent();
        objParent.sysout();
        objParent.displayName();
    }
}

interface Printable {
    void sysout();
}

class Parent implements Printable {
    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 someone explain the same?

Upvotes: 11

Views: 52161

Answers (6)

christianhs
christianhs

Reputation: 81

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();

Upvotes: 4

T.J. Crowder
T.J. Crowder

Reputation: 1074305

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.

Upvotes: 10

amit
amit

Reputation: 1

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.

Upvotes: 0

JoshuaTree
JoshuaTree

Reputation: 1261

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.

Upvotes: 0

asifsid88
asifsid88

Reputation: 4701

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

((Parent)objParent).displayName();

Upvotes: 2

PermGenError
PermGenError

Reputation: 46408

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.

Upvotes: 1

Related Questions