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
Joined: Sep 18, 2005
Joined: Jan 29, 2006
Ohh, Good explanation Marc...
Joined: Jan 29, 2003
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