Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inner class

 
Basanti Mathad
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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 {
}
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
  • 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
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
ya i meant to say that but i didn't.........
thank u........
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic