0

Difference between method overloading and overriding in java? does not give the correct answer. Below is java code.

Parent class

public class Parent {
    void display() {
        // some code
    }
}

Child class

public class child extends Parent
    void display(int a) {
        // some code
    }
}

Question: Is this Method overloading, overriding or none?

Community
  • 1
  • 1
Naman Gala
  • 4,670
  • 1
  • 21
  • 55
  • None, and you can check it by placing `@Override` at the method to override to let the compiler check for yourself. – Smutje Jun 24 '15 at 07:03
  • I am confused between override and none. – Naman Gala Jun 24 '15 at 07:04
  • No need to be confused, your example does neither overload nor override. But if you want to let the compiler check if a method is overridden, you can place `@Override` and the compiler tells you. – Smutje Jun 24 '15 at 07:06
  • @Smutje, Sorry I am confused between overload and none, but now its clear. Thanks – Naman Gala Jun 24 '15 at 07:08
  • @MarounMaroun, marked duplicate answer gives wrong answer for my question. Can you please update it. – Naman Gala Jun 24 '15 at 07:57

2 Answers2

5

That's overloading (in child), because JLS 8.4.9:

If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
3

This is Overloading

Method Overloading - method in Same Class or different class

Method Overriding - method both in Parent Child class

Here method has different signature in both Parent and Child class

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • 1
    As I understand it if you create two methods that conflict, with the same signature, then the compiler will throw an error unless you include the `@Override` notation. – glend Jun 24 '15 at 07:07
  • @doveyg yes ur correct. But not in this case – Ankur Singhal Jun 24 '15 at 07:07
  • @doveyg: No, it's the other way around: If you include `@Override` on a method that isn't, in fact, an override, the compiler will give you an error. It does not give you an error if you *don't* use `@Override` when you should (because `@Override` was added a **long** time after the language was originally specified). Now, *within the same class*, you can't have two methods with the same name and signature (whether or not you put `@Override` on one of them). – T.J. Crowder Jun 24 '15 at 07:09
  • 2
    No, it's overloading (in `child`), see [JLS §8.4.9](https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.9). Overloads don't have to be in the same class, having one inherited and the other declared still overloads. – T.J. Crowder Jun 24 '15 at 07:11
  • @T.J.Crowder corrected, somehow missed and messed completely – Ankur Singhal Jun 24 '15 at 07:23
  • When you get an answer wrong (which we all do sometimes! :-) ) and meanwhile the correct answer has been posted, it's best to just delete your answer. – T.J. Crowder Jun 24 '15 at 07:41