aspose file tools*
The moose likes Mock Exam Errata and the fly likes jtips ques Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "jtips ques" Watch "jtips ques" New topic
Author

jtips ques

gaurav nayyar
Greenhorn

Joined: Jul 13, 2001
Posts: 9
class InheritanceTest extends Process {
int x=18;
public static void main(String [] args) {
Process p = new InheritanceTest();
System.out.println(p.InheritanceTest('R'));
System.out.println(p.x);
}
InheritanceTest() {
System.out.println(true ^ true);
}
InheritanceTest(char c) {
System.out.println(c);
}
char InheritanceTest(char c) {
c='V';
return (char)c;
}
}
class Process {
int x=9;
Process() {
System.out.println("Starting Process...");
}
char InheritanceTest(int i) {
i='S';
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??
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
gaurav
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.
Dave
[This message has been edited by Dave Vick (edited July 30, 2001).]


Dave
 
Don't get me started about those stupid light bulbs.
 
subject: jtips ques