File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Mock Exam Errata and the fly likes doubt to previously answered que Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "doubt to previously answered que" Watch "doubt to previously answered que" New topic

doubt to previously answered que

Anu Kris

Joined: Oct 01, 2001
Posts: 13
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

A simple procedure to remember is :
1) All methods are invoked according to the object type of the variable, but for that the same signature method has to exist in the reference type otherwise its a compiler error.
2) All variables (static, nonstatic) are invoked according to the reference type of the variable.
Here the reference type of p is Process and object type is InheretanceTest.
The program invokes with a char argument, and you see there is no char argument method in Process. But still compiler promotes char to int and thereby maintains its rule for method invoction.
if you add the follwoing method in Process, then the method invoked will be from InheretanceTest
char InheritanceTest(char c) {
return (char)c;

I know its confusing but if you give it some time you'll understand. Hope it helps.
Asma Zafar.
Sun Certfied Programmer for Java2 Platform.
=====i can understand both the rules of 1 and 2.but i think this stmt is misleading-
**The program invokes with a char argument, and you see there is no char argument method in Process.**
FOr meth invocation it checks the ob type in this case inheritencetest...but this method is overloaded and thus b'coz of implicit conversion chooses the meth in base calss with int arg. correct me if iam wrong
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Anu,
It might be easier to think about what the Compiler sees. When the method call is made
p.InheritanceTest( 'R' );
The compiler starts checking at the Process class for a match. If finds one in the InheritanceTest( int ) method. If now looks for overridden methods in any subclasses. In your case, neither the first example or the modified one( adding InheritanceTest( char ) to InheritanceTest class) perform any overridding. Therefore, the Process method will be executed.
The only way to stop that from happening is to override the InheritanceTest( int ) method in the subclass InheritanceTest.
I agree. Here's the link:
subject: doubt to previously answered que
It's not a secret anymore!