• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

natr...mock Q 22

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I don't know why it appeared like that.
void Q22() is a method.
 
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic