aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner class" Watch "Inner class" New topic
Author

Inner class

krussi rong
Ranch Hand

Joined: Jan 30, 2002
Posts: 62
Hi,
class A{
protected int i;
A(int i){
this.i = i;
}
}
which of the following would be a valid inner
class for this class?
Select all valid answers:
A. class B{}
B. class B extends A{}
C. class B extends A{
B(){System.out.println("i="+i);}
}
D. class B{
class A{}
}
E. class A{}
the answer is A.
but I think A,B,C are the anwser, am I right?
thanks
Krussi
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Ok... so if C was a correct answer... this would be your code:

[ Ignore the next bit of BS I type I'm wrong with my logic -- I correct myself in my next post... just scroll on down ]
if class B extended A, it would inherit an inner class called B (itself!!) just like A does. But that's not possible. So hence answers B and C are incorrect.
[ June 11, 2002: Message edited by: Jessica Sant ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Francisco A Guimaraes
Ranch Hand

Joined: Mar 20, 2002
Posts: 182

Jessica, when I tried to compile the code above, it showed the following error:

Found 1 semantic error compiling "C:/temp/Test.java":
11. B() {
^
*** Error: No match was found for constructor "Test()".

I know it was supposed to give a compiler error, but I thought it would be clearer. Did this error happened because of what you said?
Francisco
[ June 11, 2002: Message edited by: Francisco A Guimaraes ]

Francisco<br />SCJP<br />please use the [code][/code] tags when showing code.Click <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">here</a> to see an example.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Originally posted by Francisco A Guimaraes:

Jessica, when I tried to compile the code above, it showed the following error:
Found 1 semantic error compiling "C:/temp/Test.java":
11. B() {
^
*** Error: No match was found for constructor "Test()".

Hmmmmm... ok scratch that stuff I said in my first post.... although it made sense at the time.
The reason for the error message you see is because constructors are chained.

so.... for the same reason the answer B is incorrect. -- No default constuctor is provided so the compiler implicitly creates one, again it tries calling super() but there is no default constructor for the class Test.
So... Write answer -- wrong logic
[ June 11, 2002: Message edited by: Jessica Sant ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Jessica Sant:
So... Write answer -- wrong logic

Hmmm...you mean "write answer, rong spelling"?
It's important to remember that when a class is instantiated, all superclasses of that class are instantiated. This step is essential as the class that you're instantiating might have inherited data members that are initialized within the superclass. If this step wasn't taken, you could easily end up with an object that is only partially initialized.
Therefore, in any constructor, unless you specify a call to the parent class' constructor with "super," there is always an implied call to the default constructor of the parent. As class A doesn't have a default constructor, you'd have to specify the call to A's constructor explicity in any extending class in order to extend A properly.
I hope that helps,
Corey


SCJP Tipline, etc.
krussi rong
Ranch Hand

Joined: Jan 30, 2002
Posts: 62
thanks very much!
Krussi
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Originally posted by Corey McGlone:

Hmmm...you mean "write answer, rong spelling"?

be nice goof -- I was going to go back and fix it... but there's a nice bug in the UBB that if a post has a QUOTE and CODE tag... when you try to edit the post... half the post disappears...
:roll: hmph!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner class
 
Similar Threads
inner class
Inner Class..
plz explain
Inner Class
inner class