• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Overridiing

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai,
The output of the following code is 'A'.But if
we'll change m1's signature in all the classes to no argument method then output will be 'C'.why?


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 c3=new C();

c1.m1(c3);

}

}
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right now the output is C
After change it's a compile error
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your question, method m1 is getting overloaded as return type of argument in each method is different,so method with that corresponding argument is called(in you case method in class A), but in case of no arguments, m1 will get overridden and method m1 on C is called as object is type C (A c1=new C() so that type of answers you are getting.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rajitha Murthy:
The output of the following code is 'A'.But if
we'll change m1's signature in all the classes to no argument method then output will be 'C'.why?



Clearly, the differnce between overloading and overriding.

The initial situation is a clear case of overloading, because the method arguments are different, e.g. void foo(int i) and void foo(String s). In case of overloading the method executed is determined at compile time based on the type of the reference. That is, the type of c1 is A and your program prints 'A'.

Now when you change all signatures to be equal then we have a case of overrriding! A first declares m1, B's m1 overrides A's and C's m1 overrides B's. And the fun thing with overriding is that method executed is determined at run-time (by the JVM). So which method is executed is based on the class the object belongs to. That is, the class the object referenced by c1 belongs to is C, so you program prints 'C'.
[ April 20, 2006: Message edited by: Dick Eimers ]
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But one thing i didnot get is if at run time it is the object of class c for first case and if we have methods with same name but the arguments in both the methods are having subtype and super type relation then the most appropriate is called i.e method with subclass parameter ,if it is true then for first case also "c" should be output but it is printing "A"
 
Rajitha Murthy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you ver much.I under stood very well
 
Dick Eimers
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by bnkiran kumar:
But one thing i didnot get is if at run time it is the object of class c for first case and if we have methods with same name but the arguments in both the methods are having subtype and super type relation then the most appropriate is called i.e method with subclass parameter ,if it is true then for first case also "c" should be output but it is printing "A"


Given the initial code posted; The compiler first looks at the type, which is A, then it looks which methods are available to A-- just 1 void m1(A a). B has 2 overloaded versions of m1 and C has 3 overloaded versions..

Try to put all versions of m1 in A and see what happens!
[ April 20, 2006: Message edited by: Dick Eimers ]
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dick you mean to say it is decided at compile time not at run time for overloading methods.
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dick you mean to say it is decided at compile time not at run time for overloading methods.
 
Dick Eimers
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is correct.
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic