• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interface

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Got a simple doubt!
Can we instantiate a interface?Is'nt it implicitly abstract?
CAN U WRITE LIKE THIS
Runnable a = new Runnable()
Thanx
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajani,
You are correct that you cannot instantiate an interface or an abstract class.
If you attempt to, you will get a compiler error or an InstantiationError.
--liz
[This message has been edited by Elizabeth Lester (edited August 22, 2001).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Helo!
Interface must be implemented, in your case the minimal implementation is:
Runnable a = new Runnable() { public void run() { } };

------------------
Antti Barck
It Solutions Consultant -- NSD Oy
Sun Certified Programmer for the Java™ 2 Platform
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Antti Barck:
Helo!
Interface must be implemented, in your case the minimal implementation is:
Runnable a = new Runnable() { public void run() { } };


You cannot instantiate an interface
But there is a way to do it using anonymous class.Though its not exactly an instantiation, it is just that the anonymous class will have the name of the interface which it implements
[This message has been edited by Ragu Sivaraman (edited August 22, 2001).]
[This message has been edited by Ragu Sivaraman (edited August 22, 2001).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Helo Ragu, can't understand a word you say...
------------------
Antti Barck
It Solutions Consultant -- NSD Oy
Sun Certified Programmer for the Java™ 2 Platform
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You cannot instantiate an interface because they are abstarct. In the code
Runnable r = new Runnable();
its not instantiating. You are simply creating an annonymouse class (new runnable());
Hope this helps,
Vidhya.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys!
I remember myself saying: "Interface must be implemented"
Implementation is done in child class and the minimal implementation I showed, just happened to be anonymous.
Get it?
------------------
Antti Barck
It Solutions Consultant -- NSD Oy
Sun Certified Programmer for the Java™ 2 Platform
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

We can declare object references for the interface. These
object references can hold any instatnce of the class which
implements that interface. for eg.


Output:
This is from myclass1 10
This is from myclass2 20

The correct version of the method is called thru the obj ref.

Indu

[This message has been edited by INDU, BALA (edited August 23, 2001).]
[This message has been edited by INDU, BALA (edited August 23, 2001).]
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Antti Barck:
Helo Ragu, can't understand a word you say...


I am sorry antti... I cant be more clearer than that
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when exception occer?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic