overriding.
Hi folks,
I am trying to experiment on overloading.
A super class reference variable can hold sub class object.At the run time, based on object assigned the methods will be called.
Upto know it is clear for me.I had seen one piece of code which i am unable understand. Here is the code.
package inheritance;
[code]
class A{
void m1(A a){
System.out.print("A");
}
}
class Bextends A{
void m1(B b){
System.out.print("B");
}
}
class C extends B {
void m1(C c) {
System.out.print("C");
}
}
class D {
public static void main(String[] args) {
A c1 = new C();
B c2 = new C();
C c3 = new C();
C c4 = new C();
c4.m1(c1);
c4.m1(c2);
c4.m1(c3);
}
}
[/code]
The out put will be ABC.
class A{
void m1(A a){
System.out.print("A");
}
}
class Bextends A{
void m1(B b){
System.out.print("B");
}
}
class Cextends B{
void m1(C c){
System.out.print("C");
}
}
class D{
publicstaticvoid main(String[] args){
A c1 =new C();
C c2 =new C();
c1.m1(c2);
}
}
The out put for this one is A.
As per my understanding the output has to be C.
Can you guys tell where I am doing the mistake in understanding?
regards,
Ramp
[2808 byte] By [
Rampa] at [2007-11-26 16:48:22]

Not sure why you would think that. A.m1() always outputs "A" regardless of what parameter is passed in.Ted.
I did not get you Ted
Rampa at 2007-7-8 23:15:54 >

The method A.m1() will only ever output "A".Ted.
Here we are passing the object reference to method not empty.I am following up this link http://www.danchisholm.net/oct1/mybook/chapter12/exam1.html
Rampa at 2007-7-8 23:15:54 >

> The method A.m1() will only ever output "A".
>
> Ted.
I don't think that's true. he's instantiating C, but because he's passing in a reference to an 'A', the m1 method is being bound the the A.m1. were he to create c1 in a C reference, the method would be bound to the m1 that takes a C, which exists in - and would print - C
@OP: try and make your class and method names more meaningful, even for this sort of testing. it's difficult to describe what's going on without getting tied in knots over 1-letter differences!
Oops, my bad. I always get cafuddled when noobs write classes and interfaces with letters. Although george, I found your answer even more confusing than the code!! Put simply:
Yes, the output should be "C" as A is actually a reference to an object of type C. Hence calling m1() the call will be passed to C.
I doubt you're getting A, probably a caching issue or summin.
Ted.
> Oops, my bad. I always get cafuddled when noobs
> write classes and interfaces with letters. Although
> george, I found your answer even more confusing than
> the code!! Put simply:
>
> Yes, the output should be "C" as A is actually a
> reference to an object of type C. Hence calling m1()
> the call will be passed to C.
>
> I doubt you're getting A, probably a caching issue or
> summin.
>
> Ted.
yeh I just re-read it! terrible explaination!
nah, I've run it, and it does as he says. it's because of the type of variable he's putting c1 in. if you change it from A c1 = new C() to B c1 = new C(), you'll see it gets passed to B.m1
:o(I hate these kinds of things.
Man this shiiit is confusing. I can see now, now that I have open my eyes. This guy is actually on about polymorphism!! I would have got that straight away if it wasn't for the terrible code an appalling use of code tags! ( I hope I would anyway).Ted.
Hi
This problem can be understood by two basic facts--
1. The superclass never has any knowledge of its subclass.
2. In case of over-riding the subclass has all the definitions of the
overriden function and invokes the correct one according to the parameter passed.
Explanation for output A in the second code-->
There the reference of superclass A has been created
A c1 = new C();
so when there is a call for any overridden function
c1.m1(c2);
only the function m1(A a) can be called as in class A this is the only definition(remember the object reference is of superclass A).
Thats why irrespective of the object being passed as parameter only function m1(A a) gets called and outputs 'A' always.
Hope this is helpful to all my friends here....
CHEEERS...........rohit13_ait@rediffmail.com
If I understand correctly, which m1 to use is chosen at compile-time. The compiler sees object c1 as A (the fact that it refers to a C-object doesn't matter), so the compiler chooses the m1 of A.
ok.
Can you guys tell me what is the difference b/w Question 5 and Question 7. I was totally confused.
Here is the link
http://www.danchisholm.net/oct1/mybook/chapter12/exam1.html
//The compiler sees object c1 as A (the fact that it refers to a C-object doesn't matter), so the compiler chooses the m1 of A.I dont think so.
Here is my code
[code]
package inheritance;
public class SingleInheritanceSuperClass {
void superClassShow()
{
System.out.println("inside SingleInheritanceSuperShow()");
}
}
package inheritance;
public class SingleInheritanceSubClass extends SingleInheritanceSuperClass{
void subClassShow()
{
System.out.println("subClassShow()");
}
}
public class MethodOverridingMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MethodOverridingSuperClass sc ;
sc= new MethodOverridingSuperClass();
System.out.println("area"+sc.area(10,5));
sc = new MethodOverridingSubClass();
System.out.println("area"+sc.area(10,5));
}
}
EvilBro, according to you i should get 500 every time. At runtime what is the object assigned to the reference variable , depending on the object the area method should excute..
Rampa at 2007-7-8 23:15:54 >

> If I understand correctly, which m1 to use is chosen
> at compile-time. The compiler sees object c1 as A
> (the fact that it refers to a C-object doesn't
> matter), so the compiler chooses the m1 of A.
exactly. the reference type is what matters here, the actual class instantiated doesn't
well that's all that cleared up then :-)
> EvilBro, according to you i should get
> 500 every time.
No. This is different from before. This new situation would be similar to:
class A {
void m1(A a) {
System.out.print("A");
}
}
class B extends A {
void m1(B b) {
System.out.print("B");
}
}
class C extends B {
void m1(A c) { // < it says A here instead of C
System.out.print("C");
}
}
class D {
public static void main(String[] args) {
A c1 = new C();
C c2 = new C();
c1.m1(c2);
}
}
this code will indeed produce C. Note that this code is actually OVERRIDING the method m1 while the previous version of this code was OVERLOADING it. OVERLOADING is determined at compile time. As c1 could be something other than a C-object, but will always be compatible with an A-object, the method of class A is chosen. Try the following:
class A {
void m1(A a) {
System.out.print("A");
}
}
class B extends A {
void m1(B b) {
System.out.print("B");
}
}
class C extends B {
void m1(C c) {
System.out.print("C");
}
}
class D {
public static void main(String[] args) {
A c1 = new C();
C c2 = new C();
c1.m1(c2);
((A) c1).m1(c2);
((C) c1).m1(c2);
}
}
