• 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

anonymous inner classes

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I'm preparing sjcp 1.4 and I have a doubt with inner classes.
I have readed that an anonymous inner class only can implement one interface, but I can execute this code with success, and I don't understand why if I said before is true.

InterfaceImpl impl = new InterfaceImpl()
{
public void method1()
{
System.out.println("James");
}
};
impl.method1();

interface Interface1
{
public void method1();
}

interface Interface2
{
public void method2();
}


class InterfaceImpl implements Interface1, Interface2
{
public void method1(){System.out.println("metodo1");};
public void method2(){System.out.println("metodo2");};
}


The result is: "James"

Thanks in advance and sorry if I have bad english grammar
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
??? How do you call this Implementation class as an inner class?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's true that an anonymous inner class can only implement one interface. Your codeis not a counterexample to that statement because it does not declare an anonymous inner class. "Anonymous" means "has no name" but your class has a name. Continue reading and find out how to really declare an anonymous inner class.
 
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
Welcome to JavaRanch!

There is an anonymous class here, but the code is incomplete (and the lack of indentation makes it hard to see). Here is a completed version with indentation...

[ December 06, 2007: Message edited by: marc weber ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And that anonymous inner class is just subclassing a class that happens to implement more than one interface.

Henry
 
marc weber
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
In your example, the anonymous class extends the class InterfaceImpl, and the reference is upcast to that type. Yes, InterfaceImpl implements 2 interfaces, and your anonymous class inherits these implementations.

But the statement about a "single interface" is talking about the syntax for creating an anonymous class.

The syntax for an anonymous class either extends a class or implements a single interface without using keywords "extends" or "implements." The class being extended or the interface being implemented is the type named after "new" and there can only be one of these.

In your example, you extended a class. But if the type had been an interface instead, then you would have been implementing an interface.
 
mathew jonson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for the fast answers, I have not been able to answer before because I have been busy

Now, I have the concept clear and the next time I'll use the identation and the complete example
 
He's giving us the slip! Quick! Grab this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic