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

Overloading question

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following class definition,which of the following method definitions could be added after line 3
1. public class Tower {
2. public void foo( double s, String str){};
3. // Insert method here
4. }

public int foo ( double s, String str){}; //1
public void Foo( double s, String str){}; //2
public void foo ( double S, String Str){}; //3
public void foo ( String str, double d){}; //4
public void foo( double[] d, String str){}; //5

the given correct answers are 2,4,5.

My analysis.
Exactly. 2 is correct because Foo is completely different method to insert
4 is correct because (double d) diffent signature is present
5 is correct because (double[] d) different signature is present

But I think 3 is also correct like (double S) is different signature.Please clarify my doubt.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The name of the parameter is irrelevant. The type and order of the parameters is what's important.

S and s are just name.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the information wise.

So

class MockExams{
public void foo( double s, String str){};
// Insert method here
public void foo (String Str,double s){}; //here the order is changed.so valid
public void foo (String Str,double d){};//order is already exists.overloading doesn�t just care about the variable names changed.so invalid.

Now I am very clear about overloading from your help.Thanks again
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A method called "Foo" cannot overload a method called "foo". It is a completely different method name. This is legal, but it is not called overloading. Overloading means using the EXACT same method name, but with a different parameter signature.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are method overloading and method overriding?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic