wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner Class explaination? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner Class explaination?" Watch "Inner Class explaination?" New topic
Author

Inner Class explaination?

Kay Liew
Ranch Hand

Joined: Dec 26, 2003
Posts: 112
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 ]

Unity can only be manifested by the Binary. Unity itself and the idea of Unity are already two.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"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
Inuka Vincit
Ranch Hand

Joined: Aug 10, 2004
Posts: 175
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


MCP (C# application dev 70-316) 860<br />SCJP 1.4 100% SCJD (URLyBird) 378<br />MAD 100% nuts
Kay Liew
Ranch Hand

Joined: Dec 26, 2003
Posts: 112
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

Joined: Aug 31, 2004
Posts: 11343

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.)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inner Class explaination?
 
Similar Threads
Request for thought-provoking questions on abstracts and inner classes
Abstract regular inner class
Abstract Inner Class
Inner Classes
K & B 1.5 Chapter 8 self Test Q.12