• 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

jtips ques

Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class InheritanceTest extends Process {
int x=18;
public static void main(String [] args) {
Process p = new InheritanceTest();
InheritanceTest() {
System.out.println(true ^ true);
InheritanceTest(char c) {
char InheritanceTest(char c) {
return (char)c;
class Process {
int x=9;
Process() {
System.out.println("Starting Process...");
char InheritanceTest(int i) {
return (char)i;
What is the Output?
1.Prints Starting Process �, false, �S� and 18
2.Prints false, �V� and 9
3.Prints true, �V� and 9
4.Prints Starting Process � , true, �V� and 9
5.Prints Starting Process �, false, �V� and 9
6.Prints Starting Process �, false, �V� and 18
7.Prints Starting Process �, false, �S� and 9
8.Prints Starting Process �, true, �R�, and 18
9.Prints Starting Process �, true, �V� and 18

the answer is 7
but how is it??
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the process that takes places when you run the code you posted.
Your line 'Process p = new InheritanceTest();' starts to create a new instance of InheritanceTest.
1. Because there are no arguements in the call to new it invokes the no arguement constructor of InheritanceTest. The first line of InheritanceTest is not a call to this() (which would invoke another verison of the InheritanceTest cosntructor so the compiler inserts a call to super().
2. The no arg constructor of Process prints out the line "Starting Process..." then returns to the InheritanceTest cosntructor.
3. The InheritanceTest constructor thenexecutes this line:
System.out.println(true ^ true); this is the 'exclusive or' meaning it returns true if one or the other, but not both, of the arguements are true. So this line prints false.
4. The next line System.out.println(p.InheritanceTest('R'));
calls a method named InheritanceTest it is not invoking a constructor!! When it does this the compiler realizes (whether you intended it or not) that because you gave a return type to one of the methods named InheritanceTest it is now a regular method and not a constructor. Furthermore because there is a method in the superclass with the same name but different parameters you have overloaded it not not overridden it. Because of this the InheritanceTest class gets both the int version and the char version of the method and the compiler is going to look for the best match at compile time so it calls the Process method which prints "S".
5. The last line that executes: System.out.println(p.x); prints out the value of the variable x. Variables are not subject to late binding so the value printed is that of the type of the variable p and not the type of the object refered to.
I hope that helps you, if you have nay other questions let me know.
[This message has been edited by Dave Vick (edited July 30, 2001).]
Legend has it that if you rub the right tiny ad, a genie comes out.
a bit of art, as a gift, that will fit in a stocking
    Bookmark Topic Watch Topic
  • New Topic