Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extended Class And implemented interface having same method name

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 ?

like image 292
user3689836 Avatar asked Oct 21 '22 07:10

user3689836


1 Answers

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.

like image 159
drew moore Avatar answered Oct 23 '22 02:10

drew moore