• 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

OverLoading Question

 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class base{
void func(int i){System.out.println("int");}
}
class derived extends base{
void func(float i){System.out.println("float");}
public static void main(String args[]){
derived d1 = new derived();
int i = 30;
d1.func(i);
}
}
 
Sarma Lolla
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the above question why am I getting comiler error?
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this link may be will help you
]https://coderanch.com/t/240554/java-programmer-SCJP/certification/conversion-long-float[/CODE][/url]
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tony,
the link does not seems to work.

Originally posted by tony kanvas:
try this link may be will help you
]https://coderanch.com/t/240554/java-programmer-SCJP/certification/conversion-long-float[/code][/url]

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sarma, I rearranged your code:

Mehtod func in derived "override" method func in base, not "overload". When you override parent's method, you should note five items: the same return type, the smae method name, the same arguments list, more visibility(e.g. you can't override public method with protected method) and throw the subset exception of the parent's exception if needed. Hope this helps.
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sarma,
my two cents :
parameter-wise, the "int" method in base is more specific
// it takes 'int' as argument
hierarchy-wise, the "float" methid in derived is more specific
// it is defined in derived
So compiler has difficulty figure out which one to use
You can try two things to ease compiler's confusion,
1. swap position of the methods -> "int" in derived, "float" in base
2. put both methods in same clase (base or derived).
hope this help
 
Claire Yang
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chi, you're right, Sarma asked about overload--not override,
 
Sarma Lolla
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tony,
Can you give me correct link if that has some useful info that clears my doubts on overloading/overriding
Claire,
I am overloading the function here. I am not overriding it. I have changed my argument type in derived class so the overriding question shouldn't arise. How ever your five points are very clear and to the point for overriding case.
chi,
How can we solve these questions if they appear in Exam? Is there any rule how to resolve in these cases? I never thought that the compiler would look for a function which is taking a float as parameter when I already defined a function with int param and calling the function with int param.
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sarma,
I believe Tony refer to the link below,
// copy & paste the whole link should be OK
https://coderanch.com/t/240554/java-programmer-SCJP/certification/conversion-long-float
For other Qs you asked, I am afraid I won't be able to answer them fully/correctly.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rule of JLS 15.12.2.2 Choosing the most specific method tells us that the type of the classes in which the methods are declared must also be considered. Do not consider only the parameters:
Base.func(int) --> one point, the parameter int is more specific than float.
Derived.func(float) --> one point, it is declared within Derived, which is more specific than Base.
Thus there is a draw and the method call d1.func(i) is ambigous.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class base{
void func(int i){System.out.println("int");}
}
class derived extends base{
void func(float i){System.out.println("float");}
public static void main(String args[]){
derived d1 = new derived();
float i = 30;
d1.func(i);
}
}

does above compiled without error?
 
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it will and print float as answer.
Because, you have taken a float argument and the matching method in the "derived" class itself.
Thanks
Sri
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i thought it might not matter if an overriden method's return type was different?

Originally posted by Claire Yang:
When you override parent's method, you should note five items: the same return type, the smae method name, the same arguments list, more visibility(e.g. you can't override public method with protected method) and throw the subset exception of the parent's exception if needed. Hope this helps.

 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class base{
void func(int i){System.out.println("int");}
}
class derived extends base{
void func(float i){System.out.println("float");}
public static void main(String args[]){
derived d1 = new derived();
int i = 30;
d1.func((float)i);
}
}
if this is complied, you can find the answer!
 
Sarma Lolla
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Melliholic Michael,
In your example you didn't override any method. You just over loaded the methods. That is why you could change your return types for the methods.
 
Onion rings are vegetable donuts. Taste this 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