0

In overloading when we overload a method why we cant make a new method which works same as overloaded method because we have to write the same number of line of code Such as in my example...why i cant make a new method b() which multiply two numbers.

public class que {

public void a(int a)
{
    System.out.println(a);
}


public  void a(int b,int c) {
  System.out.println(b*c);

}

public static void main(String[] args) {
    que queObject = new que();
    queObject.a(5);
    queObject.a(3,4);
}

}

  • In your example, overloading makes little sense. Usually you use overloading when you have multiple methods doing something similar but with different arguments. – Eran Feb 26 '15 at 08:40

6 Answers6

1

You can make all your methods have different names. The point is you don't have to. This reduces the number of names a developer using the API needs to learn.

e.g. in the PrintWriter you have lots of methods called print and println which conceptually all do the same thing. They could have been given different names, but then you would need to know which method you wanted to call,

At runtime, each method signature is unique as it includes the return type and the non generic argument types form. i.e. in byte code the names are made unique for you.

In Java, a method cannot be distinguished/overloaded by it's return type, though in Java 6 there was a bug which allowed overloading on methods with different return types.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Does method and its overloaded method form locate the same space in memeory? – Ashutosh singh Feb 26 '15 at 11:31
  • @Ashutoshsingh methods have their own memory, overloaded or not. – Peter Lawrey Feb 26 '15 at 11:38
  • means in my above que **public void a(int a)** and **public void a(int a,int b)** both have different memory..m i right? – Ashutosh singh Feb 26 '15 at 11:45
  • @Ashutoshsingh at the byte code level these are different methods with different signatures am they have their own memory usage. One might be compiled, the other might not. There don't have many more to do with each other than any two methods in the same class. – Peter Lawrey Feb 26 '15 at 11:50
0

Nobody says you can't do this. It just isn't method overloading. That's defined as two or more methods of the same name, but with different (parameter) signatures.

Ray
  • 3,084
  • 2
  • 19
  • 27
0

method name signifies what a method does. so if you have 2 methods having same name but expects different arguments. its beneficial for the understanding of code and better design to have the same name.

so if you add another method

public  void a(int b,int c,int d) {
 System.out.println(b*c*d);
}

you basically doing the same behaviour i.e. multiplication but with more arguments. so overriding is better for understanding and good coding principles.

anurag gupta
  • 379
  • 1
  • 5
0

Consider This

public  void eat(Orange o) {
  // eat Orange
}

public  void eat(Mango m) {
  // eat Mango
}

You want different implementation on the basis of what you pass as a parameter but want to keep method name same.

For More Info --> Polymorphism vs Overriding vs Overloading

Community
  • 1
  • 1
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
0

Here's my explanation:

public class Calculator {

    // used for integer numbers
    public int sum(int a, int b) {
        return a + b;
    }

    // used for double numbers
    public double sum(double a, double b) {
        return a + b;
    }
}

In this case you don't care if you use sum method with int or double - sum method is overloaded, so it will take both int and doubles. It's much easier than using sumInts() and sumDoubles() separately.

RichardK
  • 3,228
  • 5
  • 32
  • 52
0

Method overloading is when, in a class, there are more than one method with same name but different arguments although it can have different return types (different return types is in itself is not a distinguishing feature and if only that is changed will result in a compile error).

For a complete discussion, see: http://beginnersbook.com/2013/03/polymorphism-in-java/

Why do we need it?

An example will be enlightening: Lets take the ubiquitous StringBuilder class and its append methods. They are a prime example of overloading. A specific instance of method overloading is constructor overloading as well. See the StringBuilder again: http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html.

As another example suitable to your case: we can have an appendBoolean(boolean b) and a appendString(String b) and a appendChar(char c) in the StringBuilder class as well. It is a matter of clarity and choice to either have that or just have a set of overloaded append methods. To me - since the operation is to append but we are appending different instance types - having an overloaded append makes sense and provides clarity and is concise. On the other hand, you have no such choice for overloaded constructors: they need to have the same name as the class - that is by convention and by design :-)

Khanna111
  • 3,627
  • 1
  • 23
  • 25