‎2008 Feb 07 6:11 AM
Hello Gurus,
i have a problem.
-
-
-
-
-
shape -
-
-
-
-
-
-
-
-
-
Rectangle -
Triangle -
-
-
-
|
-
-
-
-
Square -
-
-
-
-
-
-
-
here shape is a interface having a method Area and the classes rectangle and triangle are implementing the interface.
but Rectangle is Super class for Square class.
1. The Method Area is implemented or redefined in Square class ?
Regards,
Ravi.
‎2008 Feb 07 6:55 AM
When you look at inheritance, you check the immediate parent-child relationship.
Since rectangle class is the immediate parent of the square class, you would 'REDEFINE' or over-ride the method AREA in the square class.
This is because, there is already some code in the rectangle class, but you are re-writing the same in square class.
However, when you look in to the SHAPE-RECTANGLE relationship, since SHAPE is an interface, you 'IMPLEMENT' the abstract method "AREA" of the SHAPE in RECTANGLE.
This is because, there is no code (abstract method) in Shape, and you are writing the code for the first time in the Rectangle class.
‎2008 Feb 07 7:27 AM
Hi,
according to ur answer the Area method should be redefined in the square class right .
but in the square class definition we need to write the code
Square class
Public section:
Methods : shape~Area Redefinition.
but this method declaration is not in the Rectangle class and the signature is in the interface.
so how it wl be redefined , it may be implementing ???
‎2008 Feb 07 7:54 AM
The very reason, you are redefining is that if you dont, the rectangle class' Area method will be called. You are redefining the code of the rectangle in the square, even though you are doing for a interface method.
‎2008 Feb 07 10:17 AM
interface methods cannot be redefined , it can be implemented alone , in that case it violates the rule . how it can be done ???
Edited by: Ravi Chandar on Feb 7, 2008 11:42 AM
‎2008 Feb 07 10:55 AM
Effectively, you are telling the runtime to not execute the code in rectangle but use the one in the square when the object references a square... using polymorphism basically.
So, an interface implementation method in the square scores over the interface implementation in the rectangle, as square inherits rectangle.
I should therefore think that you have to compare the interface implementations, and not the interface definitions during multilevel inheritance (since square does not directly inherit shape, but does so through rectangle). And hence redefinitions come into picture as with any other method implementation.