• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

instantiating interface

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 !!
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this means that we cannot create object of interface anyway?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Priyam Srivastava
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Ulf Dittmer ,Burkhard and Priyam for such a gud explanation............
[ July 30, 2007: Message edited by: dhwani mathur ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic