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?
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
posted
0
Understood... Thanks!
memati bas
Ranch Hand
Joined: Jan 29, 2006
Posts: 85
posted
0
Ohh, Good explanation Marc...
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
posted
0
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
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
posted
0
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
posted
0
Oh dear how did I miss that.
Tony Morris
Ranch Hand
Joined: Sep 24, 2003
Posts: 1608
posted
0
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.