Creating object with reference to Interface

42,972

Solution 1

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.

Solution 2

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

Solution 3

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

((Parent)objParent).displayName();

Solution 4

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.

Share:
42,972
Java Beginner
Author by

Java Beginner

Updated on September 03, 2020

Comments

  • Java Beginner
    Java Beginner over 3 years

    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?

  • Java Beginner
    Java Beginner about 11 years
    But in kathy bates its like If the variable is declared as an interface type, it can reference any object of any class that implements the interface.What do they exactly mean by any object of any class that implements the interface
  • Java Beginner
    Java Beginner about 11 years
    Thanks for Reply Eclipse suggested the same
  • asifsid88
    asifsid88 about 11 years
    By that they mean it can hold any object of which implements that interface, but to access that additional methods which is defined in the implementor you need to type-cast it. See my answer
  • asifsid88
    asifsid88 about 11 years
    Welcome :) Hope that helps
  • T.J. Crowder
    T.J. Crowder about 11 years
    @JavaBeginner: It can reference any object implementing the interface, but it can only use features of that object that are defined by the interface. This is the whole point of having an interface reference: Avoiding coupling to any specific concrete class.
  • T.J. Crowder
    T.J. Crowder about 11 years
    @JavaBeginner: There is no point to doing this. You may as well simply declare objParent as a Parent.
  • Java Beginner
    Java Beginner about 11 years
    @T.J. Crowder - You mean like with out Casting
  • T.J. Crowder
    T.J. Crowder about 11 years
    @JavaBeginner: Right, but if you have to resort to casting, the odds are your code needs to be refactored. The point of using an interface is to code to the interface and not rely on features of the concrete class. If you need to use the features of the concrete class, declare your variable using that class, not the interface.