aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please Explain. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please Explain." Watch "Please Explain." New topic
Author

Please Explain.

Tamanna Mittal
Greenhorn

Joined: Jun 03, 2001
Posts: 21
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 :-)


Cheers<BR>Tamanna :-)
Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
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

Joined: Jun 03, 2001
Posts: 21
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

Joined: Mar 06, 2001
Posts: 214
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

Joined: Jun 03, 2001
Posts: 21
Thanks pal.
------------------
Cheers
Tamanna :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please Explain.