When can an object reference be cast to a Java interface reference?

Last modified on October 6th, 2014 by Joe.

When a Java object implements the referenced interface it can be cast to the Java interface reference.

Comments on "When can an object reference be cast to a Java interface reference?"

  1. Abhishek Khandelwal says:

    In other words….An interface reference can point to any object of a class that implements this interface i.e. see the example below:

    interface Foo{
    void display();
    }

    public class TestFoo implements Foo{

    void display(){
    System.out.println(“Hello World”);
    }

    public static void main(String[] args){
    Foo foo = new TestFoo();
    foo.display();
    }

    }

  2. Joe says:

    Thanks for the nice sample source code Abhishek.

  3. chandana says:

    Using anonymous Inner class concept we can create an object for an interface…..

    Here is a sample code

    interface Sample {
    public void method();
    }

    class Outer {
    Sample s = new Sample() {
    public void method() {
    System.out.println(“Inner class”);
    }
    };
    }

    public class Example {
    public static void main(String args[]) {
    Outer o = new Outer();
    o.s.method();
    }
    }

    Output: Inner class

  4. Joe says:

    Wow! Thats an excellent example Chandana. Thanks for your contribution.

  5. macfusion says:

    About chandana’s code,(Sample s = new Sample()) i thought you can’t create an instance of an interface since it has no constructor and creating an instance calls the default constructor.Please advice…great site by the way

  6. chandana says:

    Sample s= new Sample(){
    // code
    };
    It doesnt mean that we are creating an Object for Sample Interface…
    It means that an object is creating for the class implementing that Sample interface…

  7. Anu Kuriakose says:

    Thanks a lot everyone…………

  8. tulasi says:

    thanks for good explanation

  9. Prasad says:

    @chandana :
    Nice explanation with anonymous inner class. In fact it is the only place where one can find the interface kinda instantiated (which is not true anyway :)).
    It just points that the anonymous object(having no IDENTITY/ NAME) could be referred using the interface type as shown in the code.
    Gr8 work !!

  10. pankaj says:

    thanks a lot to abhishek & chandana..

  11. shaheen says:

    thanx achana,,,,, its a nice explanatioin,,,,,

  12. shaheen says:

    sorry ,,,,, thanx chandana

  13. Partha says:

    Thanks all really nice. I had never thought of Sample s = new Sample()

    Kudos to the author.

  14. Anurag says:

    hi
    as per chandana’s post:

    Sample s = new Sample() {
    public void method() {
    System.out.println(“Inner class”);
    }
    };

    here you are creating the object of annoymous class not the interface because interface can not be instantiate.

  15. Anonymous says:

    nice one…..

  16. Govardhan says:

    interface Sample {
    public void method();
    }

    class Outer {
    Sample s = new Sample() {
    public void method() {
    System.out.println(“Inner class”);
    }
    };
    }
    I DON’T UNDERSTAND THIS, AS A BEGINNER…. U SUDDENLY JUMP INTO A CLASS, USE THE INTERFACE NAME AND CREATE A REFERENCE AND HOW COME U SAY TAT DOESN’T BREAK THE RULE OF INSTANTIATING AN INTERFACE ????

  17. Govardhan says:

    moreover that class outer doen’t implement tat interface too… !!!

  18. Govardhan says:

    The post of abhhishek seems satisfying !! thanks abhishek…

  19. Govardhan says:

    wat are d restrictions on method overloading ??
    i get to c two different answers everywhere.. ?? r both true or s only 1 true ??

  20. Govardhan says:

    Two methods may not have the same name and argument list but different return types.

    In method overloading function name will be same but the argument count or type or order must vary. They can have different return type also.

    i don’t understand d 1st xplanation.. can any1 pls xplain me briefly wit a small eg.. thank u

  21. siraz says:

    this is nice, can any one help me out with how many ways we can create an instance of an interface with few senario.

  22. Anonymous says:

    Correcting the below code snippet. Adding public modifier for display method.
    interface Foo{
    void display();
    }

    public class TestFoo implements Foo{

    public void display(){
    System.out.println(“Hello World”);
    }

    public static void main(String[] args){
    Foo foo = new TestFoo();
    foo.display();
    }

    }

  23. BINOY says:

    Ex:
    interface Inter1{}
    class A{
    Inter1 in=null;
    Object obj=in;
    }
    here for interface object also super?if yes then how?
    please explain it clearly.
    Thanks in advance…..

  24. sai says:

    should’ nt the sample s=new sample() etc be in a method/constructor of outer class? i thought giving var=value in a class, not inside any of the methods/constructors would give compilation error? please clarify.

  25. kather says:

    why always creating object through interface?

    Example :

    List arryList = new ArrayList();

    why we not create like that

    ArrayList arrayList = new ArrayList();

    Advantage and explanination…….

  26. Aananthi says:

    Instance of an interface can not be creted however refrencing can be possible

    eg, List listInstance=new ArrayList();

    here List is an interface and listInstance is an reference.

  27. Aananthi says:

    And moreover u can use both List arryList = new ArrayList();
    ArrayList arrayList = new ArrayList();

    where the arrList will allows u to use the methods implemented in List and arrayList allows u to the methods implemented in ArrayList. But the practical rule is to use List arryList = new ArrayList();

  28. Aananthi says:

    Yes, you are correct aunrag interface can not be instantiate. But we can create reference
    eg, List list=new ArrayList();
    here List is an interface to make use of ArrayList() we are creating list reference and not instance

  29. Yoga says:

    Thats Great…

  30. Parman says:

    This code snippet is having a small mistake: you have to declare the method(s) of interface as public while providing the definition for it as public void display() { System.out.println(“Hello”);}. Anyway nice job, keep it up…

  31. Parman says:

    We can’t create object of an interface. Here also we are not creating any object for the interface. It is the “inner class” that is implicitly implementing that interface providing the abstract definitions for the interface.

  32. cnsuifeng says:

    The method is public default in java inteface。So we do not declare the method as public.

  33. rahiman says:

    very very much excellent . thanks

  34. nitish says:

    can we create object of interface in java?

  35. Chalapathi Katta says:

    Thanks Chandana,Today I learnt new thing that is inner class concept here we implements the interface internaly

  36. Chalapathi Katta says:

    any how l learnt some useful concepts this website…

    let me know some more concepts….

  37. Sandeep says:

    @cnsuifeng — in interface Foo the method display is public but if we don’t declare the method as public in TestFoo it will consider the scope as default and it leads to reduced visibiliy error. So the method display should be public in TestFoo. thank you

  38. Sahil kamal says:

    why we can’t create object of interface and abstract
    in java

  39. Sahil kamal says:

    no we can’t create object of interface and also
    abstract.

  40. Vikas Shukla says:

    Example given with content is very good….Really advantageous….

  41. Anonymous says:

    what it is required to assign TestFoo object to Foo reference ? if we can call the method by TestFoo object, which can be created like this:
    TestFoo testfoo=new TestFoo();

  42. Montu says:

    Because constructors in Abstract classes are protected, preventing the abstract class from being instantiated.
    In case of Interfaces, it is not even possible to define a constructor, then how is it possible to create an instance of it?
    I hope you understand that when we create an instance of a class we actually make a call to its constructor. No such calls are possible in case of interfaces, and that’s why no Instantiation.

    Thank you

  43. K.RAJARAJAN says:

    very useful..and how i want to give the link between the login.jsp to controller,and cotroller to loginservie and LoginServiceImpl,and LoginServiceImpl to Logindao and LogindaoImpl

Comments are closed for "When can an object reference be cast to a Java interface reference?".