• 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: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I thought the answer for this ques is a,b and c.
But the correct answer is a and c.what is wrong with the option b?
Thanks.......
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 {
}
 
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basanti,
I am just guessing the answer.
extends keyword is used to inherit members from superclass whereas here A is not a superclass of B.instead it is just the enclosing class.
Hope this helps
Sri
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basanti,
In class A, you define one constructor takes 'int' as parameter, <- prevent compiler to privide any other constructor.
At inner class B extends A and you does not provide explicit code about how super class will be constructed.
eg : B() { super(5); }
<- compiler then implictly insert 'super()' for you.
This cause a error as there is no A() being defined in class A.
[ January 17, 2003: Message edited by: chi Lin ]
 
Basanti Mathad
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey chi,
u mean to say for the 2nd option to work,we need to have a constructor in class B which calls super class constructor?
Thank u guys....
basanti
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For option b to work, just a constructor in class B calls superclass constructor is not enough,
// try B(){ super();}, still fails

Because A(int) defined in class A, and B is subclass of A, we are forced to provide "super(int)" in constructor of class B so compiler won't complain.

Originally posted by Basanti Mathad:
Hey chi,
u mean to say for the 2nd option to work,we need to have a constructor in class B which calls super class constructor? <- super class constructor with 'int' parameter
Thank u guys....
basanti
[ January 17, 2003: Message edited by: chi Lin ]
 
Basanti Mathad
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya i meant to say that but i didn't.........
thank u........
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic