<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Originally posted by Lisa F.:
Ok, if I change the code to:
class A {
void m1(int a) {System.out.println("A");}
}
class B extends A {
void m1(int b) {System.out.println("B");}
}
class C extends B {
void m1(int c) {System.out.println("C");}
}
class D2 {
public static void main(String[] args) {
A c1 = new C();
c1.m1(5);
}
}
The output is "C" as I would expect. Why does changing the parameter type make this difference?
I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Even though c1 is an instance of class C, the reference type is A so the overloaded methods declared in the subclasses are not accessible. For that reason, the implementation of method m1 declared in class A is invoked.
I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Originally posted by Lisa French:
...
Can the object on which the method is called be considered an argument of the method? If so, that would clear things up.
[ July 01, 2003: Message edited by: Lisa French ]
I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Originally posted by Andres Gonzalez:
I did not get your question... sorry.. can you show me what you meant with an example?
Originally posted by Lisa French:
To hopefully clarify: In the method call c1.m1(c2); can c1 be considered an argument to the method?
The reference type (not the object type) determines which overloaded method is invoked.
Overriden methods are resolved at runtime based on the object type.
I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Originally posted by bennido kool kat:
LOL ! ... Just realised mine is the same book as Lisa's ! ... No wonder it says the same thing ... Sorry ...![]()
I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
- Polymorphism applies to overriding, not overloading
- Object type determines which overriden method is used at runtime
- Reference type determines which overloaded method is used at compile time
I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Originally posted by bennido kool kat:
Hmmm ... finally, I THINK I get it ... let a noob like myself give it a go.
c1.m1(c2)
... because c1 is of class type A, the JVM will look in class A for methods called m1. Because there is only 1 method m1, this is used.
However, if the statement was instead ...
c2.m1(c2)
... where c2 is of class C. This time, the JVM will look in class C and will find 3 methods called m1, namely ...
void m1 (A a)
void m1 (B b)
void m1 (C c)
.. Because there are 3 methods called m1, the JVM THEN uses the ARGUMENT TYPE to determine which (overloaded) method to call. In this case, the argument type is class C so void m1 (C c) is called !
And this is what I think Kathy's book is trying to tell us ! The text that Lisa read is only on the latter concept, where in fact this code is the amalgamation of 2 concepts in the book.
I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
the JVM will look in class A for methods called m1
Because there is only 1 method m1, this is used.
where c2 is of class C. This time, the JVM will look in class C and will find 3 methods called m1
Because there are 3 methods called m1, the JVM THEN uses the ARGUMENT TYPE to determine which (overloaded) method to call.
and POOF! You're gone! But look, this tiny ad is still here:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|