wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding question" Watch "overriding question" New topic
Author

overriding question

Nain Hwu
Ranch Hand

Joined: Sep 16, 2001
Posts: 139
While taking Jtip mockexam, I ran into a problem. Here is the
question:

What's the result? The answer is Y. Why not X?
Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
The char will get converted to int type and so the result
will be 'Y'
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
The method in the derived class doesn't override the one in the base one because it hasn't got the same signature. Replace char with int and you'll obtain polymorphic behaviour. "I love to say that ;-)"


SCJP2. Please Indent your code using UBB Code
MONZY THARIAN
Ranch Hand

Joined: Feb 21, 2001
Posts: 63
method is invoked based on the reference and not based on Class type . but if this is the case then the output should be 'X'
kindly can one explain

Think Big . So shall you become big.
Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
Monzy, the methods are invoked on basis of the actual type
and variables are referred on basis of the reference type.
[This message has been edited by Angela Narain (edited September 29, 2001).]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
First think like the compiler:
look for the method declaration to invoke in the class denoted by the type of the variable, or its superclasses. If there are none matching declaration the compiler will give an error. In this case in class B there is a matching method, so the compiler generates the class file and go for a deserved rest.
Now think as the JVM executing the code:
Is the method to invoke (the one determined by the resting compiler) private or static? if yes invoke it. If not it looks for any overriding method in the class of the actual object hold in the variable (A) and invoke it. But here there is no such method, so the base one is called.
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Wonderful question.
Easy trap, to make mistake
This code is nuthing but an Overloading
Just that it happened to be in the subclass instead of same class
Ragu
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
More precisely, it's overloading only for the subclass instances (and its subclasses) where the overloading method was declared, not for any of its superclasses.
Jennifer Warren
Ranch Hand

Joined: Aug 24, 2001
Posts: 53
This more interesting;
Have look at this code as it is still printing Y, Why!.
class Why{
public char m1(int x) {
return 'Y';
}

public char m1(char x) {
return 'X';
}
}

class WhyDemo{

public static void main(String[] args) {
B b = new A();
char c = 'V';
System.out.println(b.m1(c));
}
}

Thanx in advance.
Jennifer.
Elizabeth Lester
Ranch Hand

Joined: Aug 14, 2001
Posts: 129
Hi Jennifer,
What is contained in classes A and B?
--liz
------------------
Elizabeth Lester
SCJP Dreamin'
Nain Hwu
Ranch Hand

Joined: Sep 16, 2001
Posts: 139
Thanks, Jose. You explained it very clearly.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
to Jennifer:
The class Why is not used in WhyDemo.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overriding question
 
Similar Threads
danchisholm.net august09 comprehensive exam4.Q9
Compile-time Constants
4m Dan's Exam
question abt overloading frm Mock Test
Overloaded Methods in Subclass