In Java, if I have a ClassA
that extends a ClassX
and implements an InterfaceY
, and both ClassX
and InterfaceY
have a methodC()
, then why don't I have to give an implementation of methodC()
in ClassA
? Is it some kind of method overloading ?
Because if ClassX
has an implementation of methodC()
and ClassA
extends ClassX
, then ClassA
does have an implementation of methodC()
- ClassX
's implementation of it.
If ClassA
has it's own implementation of methodC()
, that would be called method overriding - ClassA
's implementation would override the implementation of its super class (ClassX
).
Overloading is a different issue entirely: Overloading occurs when a class has multiple methods with the same name/return type that take different parameters. This is useful when you want to be able to perform the same or similar operations with different inputs, and it has nothing to do with overriding, which is useful when you have a subclass whose implementation of a particular method needs to be different than that of its super-class.
See here for a helpful discussion about the differences between them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With