This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner Classes" Watch "Inner Classes" New topic
Author

Inner Classes

Kevin Lam
Ranch Hand

Joined: Oct 27, 2005
Posts: 68
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

Joined: Aug 03, 2002
Posts: 7729
Please wrap your code in brackets to preserve formatting.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Joseph Clark
Ranch Hand

Joined: Sep 10, 2005
Posts: 48
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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Sep 10, 2005
Posts: 48
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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joseph Clark
Ranch Hand

Joined: Sep 10, 2005
Posts: 48
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

Joined: Aug 03, 2002
Posts: 7729
"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

Joined: Oct 27, 2005
Posts: 68
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
 
 
subject: Inner Classes
 
Similar Threads
Request for thought-provoking questions on abstracts and inner classes
Inner Class explaination?
Abstract Inner Class
How can toplevel class be abstract?
K & B 1.5 Chapter 8 self Test Q.12