• 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

Inner class

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
krussi rong
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks very much!
Krussi
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic