Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

jtips ques  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!