Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interface question

 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
MathuKollappan Kollappan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces can't have instance variables (non-static fields) or instance initializers.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Understood... Thanks!
 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh,
Good explanation Marc...
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correction
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh dear how did I miss that.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic