Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

natr...mock Q 22

 
madhuri vl
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. class superclass{
2. static int i=5;
3. superclass(){
4. i++;
5. Q22();
6. }
7.
8. void Q22(){
9. i++;
10. }
11. }
12.
13. class Q22 extends superclass{
14. void Q22(){
15. super();
16. i++;
17. }
18.
19. Q22(){
20. System.out.println(" superclass constructor called "+(i++));
21. }
22.
23. public static void main(String[] args){
24. new Q22().Q22();
25. System.out.println("i = "+i);
26. }
27. }
a. Compile time error at line 4.
b. Compile time error at line 5.
c. Compile time error at line 15.
d. Compile time error at line 24.
e. Compile time error at line 25.
e. No compiler error
Answer given is c.
Can anybody explain this .Thanks in advance.
 
Ishaan Mohan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiler error at 15 becoz of super(). You can made call to super() or this() from a constructor only(first statement in a constructor).
void Q22()
{
super();
i++;
}

As in your code void Q22 is a function not a constructor you cannot made call to super().
 
madhuri vl
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh, I haven't noticed that .
I should wide open my eyes and see. As the coding is too big we do not think about simple tricks.
Thank you .
 
sabyasachi mondal
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ishaan,
Is void Q22() is a function or what? Pls clarify further.
Thanks
M

Originally posted by Ishaan Mohan:
Compiler error at 15 becoz of super(). You can made call to super() or this() from a constructor only(first statement in a constructor).
void Q22()
{
super();
i++;
}

As in your code void Q22 is a function not a constructor you cannot made call to super().

 
madhuri vl
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sabyasachi,
void 22(0 is a method and so you can not use the keyword super.
Super should be used in constructors and that too it should be the first sentence.
Hope you got it.
 
madhuri vl
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I don't know why it appeared like that.
void Q22() is a method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic