Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Inner Class..

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

I think a,b,c is the answer. can anyone verify this.
Thanx.
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A and C are correct.
With D and E, the compiler would complain with a message to the effect that class name A is already defined in scope.
B does not define the proper constructors.

[This message has been edited by Tony Alicea (edited February 07, 2000).]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(b) is incorrect, as the B() constructor, supplied by default, will by default call super() which is A(). This is not defined, and since A(int) is defined, A() will not be generated by default. So (b) will not compile. If you want to make a class B which extends A, it must somehow specify that it's using the B(int) constructor, like this:
<code><pre>class B extends A{
B() {
super(0);
}
}</pre></code>
or
<code><pre>class B extends A{
B(int x) {
super(x);
}
}</pre></code>
So, only answers (a) and (c) are correct for this problem.
[This message has been edited by Jim Yingst (edited February 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
only "a" is correct "c" will be correct as long as u don't create any instance of inner class B.Am i right!!!
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the problem with c? It should work fine. Try compiling and running a test program to see. Or let us know what you think the problem is - without knowing what you're thinking, I can't really say much other than "no".
Aside to Tony - hey, no fair changing your answer after I replied! We need time as well as date on the "Edited by" message so that unscrupulous posters can't get away with this sort of thing.
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to compile with the above options for inner class B. But option A) itself compiler error.
class A {
protected int i;
A(int i) {
this.i = i;
}
class B {
}
public static void main(String args[]) {
A.B ab = new A().B();
}
}
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thiru, your code didn�t compile for 2 reasons:
  • the call to the A constructor didn�t have any arguments
  • the call to the B constructor should be new B()


  • here�s a code that works:

    Hope I�ve helped.
    Francisco
     
    reply
      Bookmark Topic Watch Topic
    • New Topic