• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Nested class

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Following are the two questions from Barry Boone's Mock test.
1) class A {
protected int i;
A(int i) { this.i = i; }
}
which of the following will 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{}
My Answer: a & c. But i saw in a previous post that the answer is b) also. Can a nested class extend the outer class?
This is the first question of the Barry Boone's test (i have a paper copy of this). I would appreciate if anybody could let me know where the answers for this test are. The only clue i have is that the previous link ended with JavaCert2.html.
Thanks,
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here answer b) is NOT CORRECT but not for the reason you mentioned. An inner class can extend any class as long as the 'extended class' is accessible . The outer class A does not have the default constructor. So class B CAN NOT extend it. If A(){} would have been defined, then ans b is also correct.
regds
maha anna
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought I had read somewhere or ran across it in an mock test that an inner class could not extend an outer class and then I came across something else that said you could. After reading your post, I had the brilliant idea of writing some code to test it:
class TestInner {
public static void main(String args[]) {
new TestInner().new TestInner2().printSomething();
new TestInner.TestStaticInner().printSomething();
}
void printSomething() {
System.out.println("TestInners print");
}

class TestInner2 extends TestInner {
void printSomething() {
System.out.println("Inner class print");
super.printSomething();
}
}
static class TestStaticInner extends TestInner {
void printSomething() {
System.out.println("Static inner print");
}
}
}
This code compiles and runs successfully on my 1.2 JVM so I guess you can extend an outer class in an inner class. But then I have to ask in what situation would one want to do this?
hey, i just got promoted to ranch hand, does that mean i'm certified now?
[This message has been edited by Joe Java (edited March 07, 2000).]
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel c is also wrong for the same reason mentioned by Anna
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
shan,
ans c) has got some typo. The braces do not match. If that is corrected then ans c) is PERFECTLY correct.
regds
maha anna
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shan- the difference is, in c) class B does not extend A as it does in b). Since it doesn't extend A, it doesn't try to call new A() as a default super-constructor, so there's no problem. c) is correct.
 
Anju Rao
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies. Appreciate it.
reply
    Bookmark Topic Watch Topic
  • New Topic