Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner Classes

 
Kevin Lam
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following,

57 22 <- correct answer

45 38

45 57

An exception occurs at runtime.

Compilation fails.


But why? I thought instance t was never instantiated properly as it doesn't provides the abstract inner class implementation, so you shouldn't be able to use the method in it?

Well, even if you have done:

AbstractTest.Bar f = t.new Bar() {
public int getNum() {
return 57;
}
};

that only means f is instantiated, but t shouldn't have... dunno, confused


Help please.

Kev
[ October 30, 2005: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please wrap your code in brackets to preserve formatting.
 
Joseph Clark
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gee, that sure is an interesting example. But, there wasn't anything of that level of difficulty on the exam, was there?

I'm not sure I understand your doubt, but, obviously we're overriding
the "public getnum()" that's not enclosed in the inner class definition of Bar() when we instantiate t with the anonymous inner class.

I'm not sure I would've thought it possible to create an instance of AbstractTest() for the fact that we're not supposed to be able to instantiate abstract classes, but obviously we did.

I am studying for the 1.4 exam, and the K&B book, chapter 8, is my sole study source on inner classes. I am going to look and see if this is addressed.
[ October 30, 2005: Message edited by: Michael Clark ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make the object referenced by variable t you do not need an instance of a subclass of Bar at all. AbstractTest's default constructor is called, but that does nothing. (In fact, AbstractTest does not, in this case, need to be declared abstract because it has no abstract methods - same goes for Bar).

So you do get an instance of AbstractClass assigned to t.
 
Joseph Clark
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The K&B book states that t does not refer to an instance of AbstractTest(), but to an "anonymous (unnamed) subclass of" AbstractTest(). Assuming the anonymous subclass is implicity
abstract, we do not have to bother with any implementation of the abstract inner class Bar(). In the same way, we are not req'd to provide implementation to the members of an absract parent-class where the child-class is also abstract.
[ October 30, 2005: Message edited by: Michael Clark ]
 
Rob Spoor
Sheriff
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But anonymous classes can never be abstract, that's why you need to implement any abstract methods.

But I think the reason you can do this is that, when you create an anonymous AbstractTest subclass instance, you do not create a Bar (subclass) instance.
 
Joseph Clark
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct (as far as the anonymous not being implicitly abstract), because this code . . .


produces this error . . .

*******************************
[user@computer java]# javac AbstractTest.java
AbstractTest.java:12: <anonymous AbstractTest$1> is not abstract and does not override abstract method getNum() in AbstractTest
AbstractTest t = new AbstractTest() {
^
1 error
*******************************
[ October 30, 2005: Message edited by: Michael Clark ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"t does not refer to an instance of AbstractTest()"

Of course it does, any subclass is an instance of its superclass. Try t instanceof AbstractClass - it returns true.

When the instance of the anonymous class (a subclass of AbstractTest) is created AbstractTest's default constructor is called - it does not refer to Bar. So there is no need (at this point) for a subclass of Bar to be defined.
[ October 30, 2005: Message edited by: Barry Gaunt ]
 
Kevin Lam
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx people, so I guess the ultimate question would be, do I only need to implement the abstract methods in an abstract class to have the class instantiated and uses its methods?

So I guess I don't need to care about whether it's got inner abstract classes or not? Is that correct? If that's the case, then I understand it, otherwise... ???



Kev
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic