d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Anonymous Class
Add Reply New Topic New Poll
Member
Posts: 4
Joined: Nov 6 2013
Gold: 0.00
Dec 4 2013 04:06am
I read about object of an interface. But really it is an object of a class with no any name.
How it is possible to have a class with no any name when we try to create object for interface.

InterfaceName obj = new ImplementedClassName();

and

InterfaceName obj = new (InterfaceName) AnyClassName();

what is happening here?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 4 2013 07:03am
Technically they do have names, but you cannot reference them. but your examples aren't anonymous classes

To use an anonymous class that implements an interface, you'd use the syntax:
InterfaceName obj = new InterfaceName(){
// methods here
}

Your examples are just using the interface as a reference type. In your second example, you will get a classcastexception if the class doesn't implement the interface. implementing the methods is insufficient.

This post was edited by carteblanche on Dec 4 2013 07:04am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll