aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instantiating interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instantiating interface" Watch "instantiating interface" New topic
Author

instantiating interface

amal shah
Ranch Hand

Joined: May 05, 2006
Posts: 92
Hello,
I am not able to get the following concept....

class a
{
public static void main(String args[])
{
new abc()
{
public void method()
{
}
};
}
}

interface abc
{
}

In the main method how am i able to instantiate an interface....the concept so far i know is interfaces cannot be instantiated....

help appreciated

cheers
amal shah
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39578
    
  27
Originally posted by amal shah:
In the main method how am i able to instantiate an interface....


It looks like you're doing that, but what's really happening is that you're creating an object that implements the interface. It wouldn't work if you didn't supply implementations of all methods that are defined in that interface.


Ping & DNS - updated with new look and Ping home screen widget
Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169


here you are not instantiating an interface but you are creating an anonymous sub-class of interface abc


the above syntax means you are creating a class that has no name but implements interface abc and in that anonymous(no name) class you are defining your own function method().

refer to anonymous classes for more details !!


"History would be kind to me, for I intend to write it."
Vilas Lawande
Ranch Hand

Joined: Nov 07, 2006
Posts: 127
this means that we cannot create object of interface anyway?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39578
    
  27
Originally posted by Vilas Lawande:
this means that we cannot create object of interface anyway?


Only classes can be instantiated. What the code above does is to create an anonymous class that implements an interface. That's pretty close.
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Hey Priyam!!!Thanks for such a gud explanation,but

i have some other doubt,please if you could explain me....,here you said interface
is creating a anonymous sub class with no name
thats ok,but the method in the class

new abc()
{
public void method() { }
};

here it is method of class which is anonymous ,
but for suppose there are 2 or 3 methods
defined
in the interface than is it compulsory for us
,to implement these methods?


one more doubt i have that is if
interface consist of some method
say
public void disp();

and if we create an anonymous subclass of
interface than is it possible to implement
the interface method in the anonymous inner class?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39578
    
  27
Originally posted by dhwani mathur:
here it is method of class which is anonymous

There's nothing anonymous about the method - it is called "method", and must be so defined in the interface. Only classes can be anonymous.

but for suppose there are 2 or 3 methods defined in the interface than is it compulsory for us ,to implement these methods?

All methods that make up the interface need to be implemented.

one more doubt i have that is if interface consist of some method
say "public void disp();" and if we create an anonymous subclass of
interface than is it possible to implement the interface method in the anonymous inner class?

Isn't that exactly what the example above does, with "disp" replaced by "method"?

You seem to be confused by Priyam writing
interface abc{
}

when what he actually meant was
interface abc{
void method();
}

[ July 27, 2007: Message edited by: Ulf Dittmer ]
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Vilas asked:
this means that we cannot create object of interface anyway?

You cannot instanciate an interface (or an abstract class). But you can assign a variable of the interface's type to an object implementing that interface. E.g. String implements Comparable, so you can say:
Comparable shnook = new String("Howdy");
System.out.println(shnook.compareTo("Cowboys")); // prints 5


The method compareTo is implemented by class String.





When creating an anonymous class like
it is like you said
Runnable runner = new anonymous_class implementing Runnable { class code ...}



Bu.
[ July 27, 2007: Message edited by: Burkhard Hassel ]

all events occur in real time
Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169
hi dhwani !sorry to reply late...i had my SCJP paper so i couldn't reply sooner..

but for suppose there are 2 or 3 methods
defined
in the interface than is it compulsory for us
,to implement these methods?

one more doubt i have that is if
interface consist of some method
say
public void disp();

and if we create an anonymous subclass of
interface than is it possible to implement
the interface method in the anonymous inner class?


an anonymous implementer/sub-class has to follow all the contracts that a
normal implementer/sub-class follow..the only difference being..it simlply
doesn't have any name..
and one thing more you need to keep in mind that unlike normal(not anonymous)
implementer/sub-class you cannot implement more that one interface at a time !!
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Thanks a lot Ulf Dittmer ,Burkhard and Priyam for such a gud explanation............
[ July 30, 2007: Message edited by: dhwani mathur ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: instantiating interface
 
Similar Threads
Inheritance and exception
Interface containing an abstract method.
Inheritance Query
Inner class and misc Q
is it dynamic binding?