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,570
  • 1
  • 19
  • 50

2 Answers2

4

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
  • 959,406
  • 173
  • 1,780
  • 1,769
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
  • 25,030
  • 13
  • 76
  • 111
  • 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