• 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

Inner Class explaination?

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Extracted from KB Chapter 8: Question 12.



I thought it will fails to compile because t didn't know what it is. Can some explain the flow for me especially in t.getNum() ?? Please do not use an exact explaination in the KB book already has. I do not get it, that's why I am looking for explaination from different angle or different words.

Inner class is sooooo hard !! I am giving up this chapter ..

Thanks in advance,
K

[ September 12, 2004: Message edited by: Kay Liew ]

[ September 12, 2004: Message edited by: Kay Liew ]

(Fixed indentation I hope)
[ September 13, 2004: Message edited by: Barry Gaunt ]
 
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
Inner classes can seem tricky at first, but don't give up!

The outer class is AbstractTest. But as we'll see, this question is really about anonymous inner classes -- not abstract classes.

So let's take a look... Inside AbstractTest, we have a method called getNum which is implemented to return the int 45. We also have a definition for an inner class called Bar. Inside Bar is a method called getNum, which is implemented to return the int 38.

Here's the trick: In main, it looks like we're creating a new AbstractTest object. But actually, we're creating an object based on an anonymous inner class and upcasting that object to AbstractTest.

When you create an anonymous inner class, you essentially call the constructor of an existing class -- but after the constructor's arguments (where you would normally have just a semicolon), you insert a new class definition in brackets. From this, you get is an object that is automatically a subclass of the class whose constructor you called, and that object is automatically upcast from the anonymous type to the class whose constructor you called.

In this case, we have an object that extends AbstractTest, and is automatically upcast to AbstractTest. But in the anonymous class definition -- the part in brackets slipped in between "new AbstractTest()" and the semicolon -- AbstractTest's getNum method is overridden to return 22 rather than 45.

Next, we do it again: It looks like we're creating an instance of the inner class Bar; but actually, we're creating an object with another anonymous class and upcasting that object to AbstractClass.Bar. In this anonymous class definition (the brackets inserted after the constructor call), Bar's getNum method is overridden to return 57 rather than 38.

So now, when we call f.getNum(), polymorphism kicks in. The method invoked is the one associated with the actual runtime type (the anonymous class) rather than the declared type (AbstractTest.Bar). In other words, the overridden version of the method is invoked, so f.getNum() returns 57.

The same thing happens when we call t.getNum(). The overridden version is invoked, and t.getNum() returns 22.

Note the comment lines in the code below.
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like to referer to the java tutorials which are very well written
http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html

like the guy above said, dont worry about it, think of it as just an other
special set of classes. There are three kinds the one in the code is an annoymous class. I wont go on further because mark gave a pretty good description of it
 
Kay Liew
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Woah .. thank you very much guys! The explaination was detail and the reference URL from Sun was helpful. I will need sometime to digest the explanation from Marc .. Barry, sorry about the indentation, I am using VI as my text editor and from time to time I forgot to use tab key to correct the format. I will try next time before posting.

Thanks again !!

Kay
 
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
I should add 2 things...

First, instead of extending a class and being upcast to that class type, an anonymous inner class can implement an interface and automatically be upcast to that interface type. The syntax is the same, just using the interface name in place of the class name.

Second, there's a distinction between "inner" classes and "nested" classes. (See the link Benjamin provided.)
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads 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