• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

confusion about overriding

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please look at this prog
class A{
void m1(A a){System.out.println("A");}
}
class B extends A{
void m1(B b){System.out.println("B");}
}
class C extends B{
void m1(C c){System.out.println("C");}
}
class D{
public static void main(String args[]){
A a1=new A();
A b1=new B();
A c1=new C();
C c4=new C();
c4.m1(a1);
c4.m1(b1);
c4.m1(c1);
}
}
In this prog, the output should be ABC(Bcoz at runtime these methods are called).But the actual output is AAA.Explain How?
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For over-riding type of method arguments should be EXACTLY same. Hence this is an example of over-loading.

As you know, only in case of instance method over-riding dynamic method invocation comes into picture. In all other cases methods are bound at compile time and hence the o/p AAA as all the variables a1, b1 and c1 are of reference type A.

I hope this clarifies your doubt.

Murali...
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method signature m1(A) matches with that of the Parent class A....This is done by the compiler before executing...
 
sukhavasi vasavi
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Murali,
I got it
 
Hey! You're stepping on my hand! Help me tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic