Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please Explain.

 
Tamanna Mittal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain about the output.
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;
}
}
Options are:
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
And correct option is 7. but y?


------------------
Cheers
Tamanna :-)
 
Rashmi Tambe
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tammana,
A class can have a memeber method with same name as constructor, so char InheritanceTest(char) is treated as method.
Now Process p = new InheritanceTest();
first executes the base class constuctor-
Starting process
Then derived class constructor, true^true will give-
false[b]
Then comes the println stmt...
p.InheritanceTest('R')
'R' gets converted to int implecitly. there is no method InheritanceTest(int) in derived class(remember it's InheritanceTest(char) not int!!!), so it calls base class InheritanceTest(int) method. So-
[b]s

Now because of p.x , being a base class ref. p does not know 'x' in derived class. So p.x will print base class value of 'x'. So-
9
I hope this'll help.
Rashmi

 
Tamanna Mittal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rashmi,
I very well got the constructor's concept. But the method thing is still not clear.
this statement
Process p = new InheritanceTest();
will initialize the variable p to type derived class
in derive class there is a method

char InheritanceTest(char c) {
c='V';
return (char)c;
}

then y would the char will implicitely convert to int and hence will call the method of the base class. Rest all the code i am clear with but this is the only part i am not clear with. Please help.
 
Percy Densmore
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tamanna,
I saw an excellent discussion on this very question here: http://www.javaranch.com/ubb/Forum24/HTML/008132.html

------------------
Percy Densmore
-SCJP2 Wannabee
 
Tamanna Mittal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks pal.
------------------
Cheers
Tamanna :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic