Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Interface Methods can be Redefined ???

Former Member
0 Likes
838

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.

5 REPLIES 5
Read only

Former Member
0 Likes
583

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.

Read only

0 Likes
583

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 ???

Read only

0 Likes
583

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.

Read only

0 Likes
583

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

Read only

0 Likes
583

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.