• 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:

interfaces instantiated???

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interfaces can't be instantiated - the first rule you're taought about them.
but why does this break down in these examples -
1.) Runnable r = new Runnable();
// some stuff;
Thread t = new Thread(r);
t.start();
2.) addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent ae){
//
}
}
);
some clarification wanted.
also, what all interfaces can be instantiated then?
------------------
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anshuman,
1. Runnable r = new Runnable() -- this will not compile; you'll see the following error:

2. In this case you are creating an Anonymous class based on the Interface ActionListener and you are implementing the one method the interface defines: actionPerformed().
What you first learned, interfaces cannot be instantiated is true for all interfaces.
Hope that helps.
------------------

Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
 
Anshuman Acharya
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks jen... but still a problem.
as for 2, no problems... i found a link to it last night. as you say, the compiler fills in the blank for the missing "implements".
but as for the first, i have seen this code appear and pass as a correct answer in 2 mock exams... i will just code and check! but if what you say is right, then i guess you should rely more on your fundamentals than these flashes in the pans of erratta!
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
another thing you will find, which confused me at first, is sometimes a method will return an object of an interface type. for example Applet has a method
getAudioClip(URL url)
Returns the AudioClip object specified by the URL argument.
however AudioClip is an interface. What actually happens is the method creates a class which implements AudioClip, creates an object of this type, then upcasts it to the interface type(AudioClip) and returns it.
 
Anshuman Acharya
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks jen... but still a problem.
as for 2, no problems... i found a link to it last night. as you say, the compiler fills in the blank for the missing "implements".
but as for the first, i have seen this code appear and pass as a correct answer in 2 mock exams... i will just code and check! but if what you say is right, then i guess you should rely more on your fundamentals than these flashes in the pans of erratta!
 
Anshuman Acharya
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that was some interesting observation, randall! i did know that the return type of the getAudioClip was AudioClip object but it had slipped my mind that AudioClip was an interface.
All i can say is that Java's compiler seems to be pulling a lot of strings backstage than i have seen some other compilers do!
thanx
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anshuman,
You can create a reference variable for an Interface type and assign any object that implements the Interface or one of it's superinterfaces.
For example,

The above compiles without error. Maybe this is what you have been running into on some of the mocks?


------------------

Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
 
Anshuman Acharya
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks jane,
but no.
the examples i saw instantiated the Runnable interface like this :
Runnable r = new Runnable();
Thread t = new Thread(r);
i just guess that whoever made this question had his/her fundamentals all warped up!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic