• 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

Jtips - mockexam #1

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This problem is from Jtips mock exam # 1. Answer for this is 4.

public class Superclass {
public static void main(String[] args) {
System.out.println(new Subclass().methodA());
}

Superclass() {
System.out.println("SuperClass Constructor Executed");
}

private int methodB() {
System.out.println("methodB in Superclass");
return 9;
}

int methodA() {
System.out.println("methodA in Superclass");
return methodB();
}
}

class Subclass extends Superclass {
Subclass() {
System.out.println("SubClass Constructor Executed");
}

protected int methodB() {
System.out.println("methodB in Subclass");
return 1;
}
}
What is the result ?
1.Prints
a.SubClass Constructor Executed
b.methodA in Superclass
c.methodB in Superclass
d. 1
2.Prints
a.SubClass Constructor Executed
b.SuperClass Constructor Executed
b.methodA in Superclass
c.methodB in Superclass
d. 1
3. Prints
a.SuperClass Constructor Executed
b.SubClass Constructor Executed
b.methodA in Superclass
c.methodB in Subclass
d. 1
4.Prints
a.SuperClass Constructor Executed
b.SubClass Constructor Executed
b.methodA in Superclass
c.methodB in Superclass
d. 9
5.Compile-time error occurs.
It is calling methodB() of superclass and prints 9.
If I remove private modifier or override methodA() in Subclass it calls methodB() of Subclass and prints 1.
Can any one explain.
Regards,
Usha
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
see this discussion
http://www.javaranch.com/ubb/Forum24/HTML/009386.html
[This message has been edited by Thomas Paul (edited April 24, 2001).]
 
No. No. No. No. Changed my mind. Wanna come down. To see this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic