user3689836
user3689836

Reputation: 51

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 ?

Upvotes: 5

Views: 2480

Answers (2)

drew moore
drew moore

Reputation: 32680

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.

Upvotes: 2

Karibasappa G C
Karibasappa G C

Reputation: 2732

its very simple,

actually when you extend the class all non private attributes and methods gets inherited to sub class.

so in your case

class X{

//method C implemented here
}

interface Y{
//method c declaration
}

class A extends X implements Y{
//method C implemented here comes from class X
}

so no need to provide it again.

Upvotes: 1

Related Questions