• 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

please respond to this question on inner classes.

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I saw this question in one of the mock tests...I don't know whether it is already discussed here I am a recent visitor of this site ..when I tried to complie and run the code using options b & c ..it's not giving me the error...so why b & c are not given as answers here...please can anybody give me the explanation ,I am wrong or the answer given is wrong?
please respond to this....
Given the following class definition:
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 {
B() {
System.out.println("i = " + i);
}
}
d) class B {
class A {
}
}
e) class A {
}
Given answer is only A.
Regards
Krishna

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The mock test's answer is right, as innner classes shouldn't have extensions like (extends A ) or, it shouldn't have A as inner class as in Answer C.
You've mentioned the code compiled without error. While trying the code, where did you insert the answers?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The correct answer should be A and C, but not B. For B, there's nothing wrong with an inner class which uses extends - however, in this case class A has no no-argument constructor. So if class B wants to extend it, it must include a B constructor which explicitly calls super(int) on the first line - otherwise the default is a call to super(), which does not exist. I don't see how you could get this option to compile without changing it - could you show the code you used, Krishna?
Answer C is correct though - the mock exam is wrong here:
<code><pre>
class A {
protected int i;
A(int i) {
this.i = i;
}
class B {
B() {
System.out.println("i = " + i);
}
}
}
</pre></code>
This compiles without error.
[This message has been edited by Jim Yingst (edited August 25, 2000).]
 
Krishna Shubha
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much karsthish and Jim for your kind reply...I am sorry jim I got the mistake I have done while compiling using second option,actually I mistakenly defined default constructor in the super class i.e A,so I didn't get any compiler error...I am really so thankful to you for responding to this..I am going to take the exam tomorrow...
Regards
Krishna
 
reply
    Bookmark Topic Watch Topic
  • New Topic