-1

I am really confused as to how overriding is very different from overloading in java since when I implement overloading I call a method by the same name but different signature that is a different datatype parameter or a different return type. In overriding also I call different methods(Different block of code) by passing objects of different type. Now when I am passing different types of objects its the same as passing a different datatype as different classes represent different user-defined datatype. So apart from the difference of Overloading being in the same class and overriding being in different classes are the two conceptually the same?

Akash Singh
  • 21
  • 1
  • 8

7 Answers7

2

Override means that you change the functionality of a method (you overwrite what that method does).

Overload means that you keep the method name and return type , but you have different input paremeters.

The two concepts are very different.

While in override you have a different functionality for the SAME method, with overload you have two DIFFERENT methods with the SAME name .

!EDIT

You can overload a method to have a different return type only if you have different input parameters. You cannot overload a method by only changing it's return type.

MichaelCMS
  • 4,703
  • 2
  • 23
  • 29
  • You can change the return type. – ChiefTwoPencils Jul 31 '14 at 08:38
  • Yes, you can only change the return type if you also change the input parameters. I wanted to underline that you cannot change the return type without changing the input parameters. Going to edit. – MichaelCMS Jul 31 '14 at 08:53
  • Yes, the return type is not part of the method signature. This is because there is no way to differentiate at compile time two method calls with the same parameters but with different return types, if you don't assign the return values to any variables. You can always call a method just for the side-effects after all. – papacito Jul 31 '14 at 09:11
0

The most significant diference is that overriding works at runtime and overloading at compile time.

That is the reason, that lots of people in the begining (me too) don't understand that polymorphism do not work for overloaded methods.

PeterMmm
  • 24,152
  • 13
  • 73
  • 111
  • I am really confused as to how we say that the signature is the same in overriding when we pass different types of object(objects of different type). Don't they represent two different datatypes (User-defined datatypes)? Thus making the two signature different. – Akash Singh Jul 31 '14 at 08:40
0

No, they aren't the same concept.

When overriding you change the normal behaviour of existing method.

When overload you add a new method that will do or is expected to do the same things in background. If you do an overload you want to have the chance of call the same method with different arguments to do same thing, not to change the behaviour.

I think that they aren't conceptually the same.

inigoD
  • 1,681
  • 14
  • 26
0

When you overload a method, you allow the method to be invoked through different signature..

For example

add(int a, int b);
add(int a, int b, int c);  
add(int a, int b, int c, int d);    

As you see here, all the three overloads are having different signatures..

However, when you over-ride a method, You must have the same signature as the method you are trying to override..

Hence overloading requires same name whereas over-riding requires same signature, but different functioning..

Gijs Overvliet
  • 2,643
  • 3
  • 28
  • 35
Manish Kr. Shukla
  • 4,447
  • 1
  • 20
  • 35
  • `requires ..., but different functioning` not real. You can override and only call super and you have the same method twice. Or even you can have overriden methods with empty body at all. – PeterMmm Jul 31 '14 at 08:45
  • You're Correct @PeterMmm... Putting it in different words, "You can", but its not that "You must".. – Manish Kr. Shukla Jul 31 '14 at 08:47
0
public class animal{

public String sayHi(){
      return "hi im animal";
  }
}


 public class dog extends animal{
//you are overriding the method by changing its funcionality instead of returning "hi im animal" it will return  "hi this is dog"
  public String sayHi(){
          return "hi this is dog";
      }
}


public class cat extends animal{
//you are overloading the method by adding a parameter catName.
 public String sayHi(String catName){
              return "hi this is "+ catName;
          }
}

overriding and overloading use the same method name but different functionalities,

overloading as the name implies you are putting some stuff(parameters) inside the method. it is like a stuff toy. overloading with cotton so it could be fluffy.

this is polymorphism which is a single method but with different behavior.

Ker p pag
  • 1,568
  • 12
  • 25
0
  • Overloading happens at compile time

    Another important point to remember is that overloading is a compile time phenomenon. This just means that the compiler determines whether a given method(s) is correctly overloaded.

  • Overridden method provide virtual method invocation feature.

  • Overriding methods is completely different from overloading methods. If a derived class requires a different definition for an inherited method, then that method can be redefined in the derived class. This would be considered overriding.

  • An overridden method would have the exact same method name, return type, number of parameters, and types of parameters as the method in the parent class, and the only difference would be the definition of the method.

Read more about Polymorphism

So apart from the difference of Overloading being in the same class and overriding being in different classes are the two conceptually the same?

No, method overloading can span in multiple classes as well. You can provide another method with the same name in child class.

Learn more...

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • I am really confused as to how we say that the signature is the same in overriding when we pass different types of object(objects of different type). Don't they represent two different datatypes (User-defined datatypes)? Thus making the two signature different. Or am I not clear about the concept of user-defined data types? – Akash Singh Aug 01 '14 at 13:05
  • what do you mean by different type of object. Do you mean `Number` instead of `String`. – Braj Aug 01 '14 at 13:07
0

Thank you @MichaelCMS for answer. I added some sample code. I hope it helps.

public class Base {

    public void sayHello() {
        System.out.println("Base.sayHello()");
    }

}

public class Child extends Base {

     /*If you remove the comments, you can see that overriden method is called.*/
    // this is method overriding
//  public void sayHello() {
//      System.out.println("Child.sayHello()");
//  }

    //this is method overloading
    public void sayHello(String name) {
        System.out.println("Child.sayHello()-> " + name);
    }


    //This is method overloading
    public int sayHello(String name, String surname) {
        System.out.println("Child.sayHello()-> " + name + " " + surname );
        return 0;
    }

    public static void main(String[] args) {
        Child child = new Child();
        child.sayHello();
        child.sayHello("Filiph");
        child.sayHello("Filiph", "Jenssen");

    }

}

prints out

Base.sayHello()

Child.sayHello()-> Filiph

Child.sayHello()-> Filiph Jenssen

Community
  • 1
  • 1
faraway
  • 862
  • 15
  • 27
  • 1
    method `sayHello()` is overloaded in multiple classes, that don't prove your last statement. overridden method is commented. – Braj Jul 31 '14 at 09:04
  • Yeah I commented overriden method on purpose. I mean if you remove the comments, you gonna see the overriding method is called. Let me describe it in answer – faraway Jul 31 '14 at 09:18