• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Overloading and Overriding

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can someone explain this code


class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}
void m1(A a) {System.out.print("A in B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D1 {
public static void main(String[] args) {
B c1 = new C(); C c2 = new C(); c1.m1(c2);
}}


It displays B as output.

How does it work when both overloading and overriding are present in the same code


Thank you

Gayatri
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI gayathri,

I think u r missing the difference between overlaoding and overriding
Here its jus overloading (all diff arguments) ..so the answer

if u have concerns lemme know

regards
ganesh
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s ganesh all the methods are overloaded.....
my doubt is y the output is "B" and not "A in B".

is it because c1 is of type B, void m1(B b) method is invoked rather than
void m1(A a). I dont know whether i am right.....

pls clarify.....
 
ganesh subbiah
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ramya,

In overloading, the function to be called is decided in compile time .It is the Object type that is important here, so c1 being B type , it has too call m1(c2) . In Overloading, the compiler chooses the most specific method
hence m1(B b ) is more specific than m1(A a) in this call -m1(c2)

I think ramya this makes you clear of the result
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, i agree that the funtion to be called is decided during compile time, and here the compiler decides to call m1(B b) of class B,
but finally at runtime the call is based on the object and so the class c's method should be called, which should result in the output as "C"
Please explain me, i am confused :roll:

Thanks
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's why it acts the way it acts:

When it comes to overloading, the REFERENCE TYPE determines which overloaded version is selected. So, obviously, this happens at compile time.

When it comes to overriding, the OBJECT TYPE (the actual type of instance) determines which method is selected. So, happens at runtime.

In this case, the object was created like this:
B obj = new C();
Since this is a case of overloading of methods, the REFERENCE TYPE will be considered for invoking the method.
So, the method which accepts B as the parameter is invoked.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once javac has chosen at compile time to call a method with the signature "m1(B)", the jvm will only choose between methods with that specific signature during execution.

Only overiding operates at execution time. As Agni pointed out, the choice between overloaded methods is always made at compile time based on the exact reference type used to invoke the method. If that class or interface doesn't declare or inherit a method signature, that signature will not be chosen. This is true even if a subclass has a method with that signature. Remember, you can't inherit up.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi



i doubt how line 1 and line 2 does not come under overriding??
pl explain
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question that started this thread is why this method
void m1(C c) {System.out.print("C")
in class C was not called by "c1.m1(c2);"

The answer is that c1 is reference type B and no method with the signature "void m1(C)" is declared or inherited in class B.

Between the overriding versions of "void m1(A a)" in class A and class B, java picks the version in class B because c1 refers to an object in class B.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic