-2

As i know, in overriding we can't change the signature of the method. If will try to change, it will through a Compile time error.

So can anyone help me to understand why i am not getting error in below code. enter image description here

Deepak Malav
  • 306
  • 1
  • 5
  • 13
  • 1
    No. That would be overloading. – ernest_k Feb 14 '19 at 17:31
  • 1
    Add code as text not as image – Jens Feb 14 '19 at 17:32
  • @ernest_k Can you please explain ? As far as i know that is overriding and if i will change the method signature it should through CTE. – Deepak Malav Feb 14 '19 at 17:33
  • @DeepakMalav What makes you say it's overriding? What's do you understand as difference between overriding and overloading? – ernest_k Feb 14 '19 at 17:35
  • @DeepakMalav you are not overiding but you create a second, additional method in the child with a different signature. So in Child both methods exist next to each other - without conflict as their signatures differ. Therefore this is perfectly valid. – kai Feb 14 '19 at 17:40
  • 1
    @kai Can we change method signature in overriding ? – Deepak Malav Feb 14 '19 at 17:47
  • no you can't. As you don't override but create an additional method that coexists everything is perfectly fine. – kai Feb 14 '19 at 17:50
  • @kai if i change the method signature in sub class method then will it become overloading? – Deepak Malav Feb 14 '19 at 17:52
  • yes multiple methods with the same name but different parameters are called "overloaded". But overloading is not overriding. – kai Feb 14 '19 at 17:56
  • 1
    @kai overloading is happening in same class but here classes are different. – Deepak Malav Feb 14 '19 at 18:02
  • Overloading is happening in the same class(both methods are there next to each other): the child. But whoever claims that it may not be called overloading if inherited methods are involved, should come up with a better name. Never heard such claim and I'd say it's impossible to uphold as you may not know the whole derivation tree(e.g. in a lib). So: Its overloading. – kai Feb 14 '19 at 18:16

1 Answers1

1

Because you changed the parameter type your example is showing overloading, not overriding.

You can add the @Override annotation to guard against accidental mistake like that:

@Override
public void testMethod(float b) {

}

will cause a compilation error.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111