• 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

Abstract inner class in the abstract outer class

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Another question from me (source http://www.certpal.com/):
I assume that when we instantiate an abstract class, we need to explicitly instantiate an inner abstract class as well? Like all other abstract members?
No?
So why then the output of this code is "filan stored" ??
If I am completely wrong, can you explain me then, please?
Thank you!!!
 
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

Anna Rozanova wrote:No?


No.

As for your other question, what do you think the output should be, and why?
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abstract classes cannot be instantiated, but they can be subclassed.

dodo2 is an instance of a subclass of the abstract class dodo. This subclass (an anonymous inner class) overrides the get() method in dodo to return filan. That's why line 11 returns filan.

b is an instance of a subclass of the abstract inner class dodo.brain. This subclass (an anonymous inner class) implements the abstract get() method in dodo.brain to return stored. That's why line 12 returns stored.
 
Anna Gabillet
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much, you helped a lot!!!
I've forgotten, that an inner class is not a member of its outer class, thus there's no need to initialize it...

So only thanks to anonymous classes we can write a code like that (with the use of abstract classes), otherwise - we would have to write a new non-abstract top-level class which would be a child of a dodo class. Is that right?
 
Paul Clapham
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
Yes, that is a good example of how to use anonymous classes, and also how they relate to inner classes. The part about "abstract" is just there to confuse you, and it doesn't affect how you would use anonymous classes. The rules about how you make an anonymous subclass of an abstract class are pretty much the same as the rules about how you make an anonymous subclass of any other class.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I've forgotten, that an inner class is not a member of its outer class, thus there's no need to initialize it...



An inner class is a member of its outer class; just like a wheel is a member of a car.

So only thanks to anonymous classes we can write a code like that (with the use of abstract classes), otherwise - we would have to write a new non-abstract top-level class which would be a child of a dodo class. Is that right?



Yes (assuming that by 'child', you mean 'subclass'). Thanks to anonymous inner classes, we can create an object of an unnamed class which extends a named class or implements a named interface.

You might find this useful http://download.oracle.com/javase/tutorial/java/javaOO/nested.html
 
Anna Gabillet
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot guys!!!
 
Goodbye moon men. Hello tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic