Meaningless Drivel is fun!*
The moose likes Beginning Java 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 » Java » Beginning Java
Bookmark "interface question" Watch "interface question" New topic
Author

interface question

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hi guys! I know that an interface is like a 100% abstract class... And an abstract class cannot be instantiated... But are there exceptions to this rule? Is there any way an interface can be instantiated?


SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
MathuKollappan Kollappan
Greenhorn

Joined: Feb 14, 2006
Posts: 1
This is the one way of initializing

interface Interface
{
public void showSomeThing();
}

class InterfaceImpl implements Interface
{
public void showSomeThing()
{
System.out.println("Interface Demo");
}
public static void main(String[] args)
{
Interface obj = new InterfaceImpl();
obj.showSomeThing();
}
}
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Interfaces can't have instance variables (non-static fields) or instance initializers.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You can instantiate a concrete class that extends an abstract class and/or implements an interface, and then upcast the reference to the type of the abstract class or interface. But you cannot directly instantiate an abstract class or interface.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Understood... Thanks!
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Ohh,
Good explanation Marc...
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Say SomeInterface has one requiredMethod(). There is syntax like:

That looks for all the world like you're instantiating an interface, but you're really defining a new class that implements the interface and instantiating the new class. The new class doesn't have the "public class name" clause so it looks pretty odd. This "anonymous class" trick is often seen with listeners for Swing events, defining them on the fly rather than putting them in their own class files.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Correction


Tony Morris
Java Q&A (FAQ, Trivia)
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Tony Morris:
Correction


That's still not going to compile on account of the fact that neither of you have implemented the requiredMethod(). Pun intended.

*grin*
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83
further harrassment....

and even then it won't compile, because you need a semicolon after the closing brace on the next-to-last line, to complete the assignment statement started on the first line....

-- Jon
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Oh dear how did I miss that.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Yeah, I carefully said the syntax is "like" what I showed. Didn't take the time to try to compile it. Thanks for the fixes, all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interface question