This week's book giveaway is in the Agile/Processes forum.
We're giving away four copies of Building Green Software: A Sustainable Approach to Software Development and Operations and have Anne Currie, Sarah Hsu , Sara Bergman on-line!
See this thread for details.
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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).]
reply
    Bookmark Topic Watch Topic
  • New Topic