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

 
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interfaces can't have instance variables (non-static fields) or instance initializers.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Understood... Thanks!
 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohh,
Good explanation Marc...
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correction
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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*
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh dear how did I miss that.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic