File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interface Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interface Question" Watch "Interface Question" New topic

Interface Question

S. Sumukh
Ranch Hand

Joined: Aug 27, 2003
Posts: 31
This is in connection with last post regarding inner classes.
I came across following code in Bruce Eckel's book..
interface A {}
interface B {}
class X implements A, B {}
class Y implements A {
B makeB() {
// Anonymous inner class:
/*problem*/return new B() {}; //How come Interface is instantiated ?
public class TestClass {
static void takesA(A a) {}
static void takesB(B b) {}
public static void main(String[] args) {
X x = new X();
Y y = new Y();
} ///:~
Everything works Ok...
But I couldnt Interface B can be instantiated ?
can neone plz explain ?
Uma Balu
Ranch Hand

Joined: Aug 22, 2003
Posts: 61
/*problem*/ return new B() {}; //How come Interface is instantiated ?

The interface B is not instantiated. An anonymous class is created, which implements the interface B.
Note that it is

and not

S. Sumukh
Ranch Hand

Joined: Aug 27, 2003
Posts: 31
Thanks Uma..
I agree. Here's the link:
subject: Interface Question
jQuery in Action, 3rd edition