Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding question

 
Nain Hwu
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The char will get converted to int type and so the result
will be 'Y'
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ;-)"
 
MONZY THARIAN
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jennifer,
What is contained in classes A and B?
--liz
------------------
Elizabeth Lester
SCJP Dreamin'
 
Nain Hwu
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Jose. You explained it very clearly.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to Jennifer:
The class Why is not used in WhyDemo.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic