override or virtual ?? in interface implementation

  • Thread starter Thread starter Romain TAILLANDIER
  • Start date Start date
R

Romain TAILLANDIER

Hi group

I have interface IMyClass.
IMyClass have a ToString() method
then i have MyClasse : IMyClass
how to declare the .ToString method in MyClass ?

i try
public virtual override ToString() {...}
public override ToString() {...}
public virutal ToString() {...}

after some reflection i think i have the good answer
I think I must not declare ToString in the interface because it is herited
from object, so i suppose i have to declare
public override ToString() {...}
in MyClass

can somebody confirm please ?

thanks
ROM
 
ToString() is part of Object, which all classes ultimately inherit from. You
only have to override the ToString() method from the Object base class for
your own implementation.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think Outside the Box!
**********************************************************************
 
thank you greg,
ToString() is part of Object, which all classes ultimately inherit from. You
only have to override the ToString() method from the Object base class for
your own implementation.

but in MyClass, the ToString method is necessary and must be override. How
can i make it appears in the interface IMyClass ? and so what about the
implementation declaration in MyClass ?

thank you for helping
ROM
 
Romain TAILLANDIER said:
but in MyClass, the ToString method is necessary and must be override.
How can i make it appears in the interface IMyClass ? and so what
about the implementation declaration in MyClass ?

I don't believe that you can use an interface definition to force someone
to override one of the functions provided in a base class of an object that
also implements that interface... In fact it doesn't even make sense -
what you are essentially saying is that 'ToString() must be implemented',
but because it is already implemented, the compiler doesn't complain
because it IS already implemented for every class you would derive (because
everything derives from System.Object).

While on this subject, I would ask - do you REALLY need this Interface (to
define functionality of many disparate objects), or would you be better
served by an object deriving from System.Object that also includes the
functions you are trying to use an interface to define. I know many many
programmers that over-use Interfaces in favor of base classes just because
'interfaces are cool'. By using a base class instead of an interface, you
could do:

public abstract class MyClass
{
public abstract new string ToString();
}

which would force anything deriving from MyClass to define their own
ToString() method.

-mbray
 
Michael Bray said:
While on this subject, I would ask - do you REALLY need this Interface (to
define functionality of many disparate objects), or would you be better
served by an object deriving from System.Object that also includes the
functions you are trying to use an interface to define. I know many many
programmers that over-use Interfaces in favor of base classes just because
'interfaces are cool'. By using a base class instead of an interface, you
could do:

public abstract class MyClass
{
public abstract new string ToString();
}

which would force anything deriving from MyClass to define their own
ToString() method.

Note that instead of hiding Object.ToString, you might want to force
overriding:

public abstract class MyClass
{
public abstract override string ToString();
}

That depends on exactly what you want the behaviour to be, of course.
 
Jon and Michael,

What is the real difference between an real interface and a abstract class ?
How that difference are reflected on the derivated class ?
In my code, what if i change the interface in an abstract class ? what
change could i have to do ?

I want to notice that i am pretty sure that in my case, I could not use
other than interface. I have some dynamic Dlls containing all the same
classes, but different implementation. I acces to that classes by
dynamically loading the assembly, found the classes, load the adapted
methods, and create an interface instance.

Do you think i just have to replace Interface by abstract class, add the
abstract new string ToString(); compile and all would be ok ?

any heavy changement would make me very late on the project....

Thank you for answering ....
ROM
 
Romain TAILLANDIER said:
What is the real difference between an real interface and a abstract class ?

That's a big question - interfaces and abstract classes are really
completely different things, but they happen to have similar properties
(in terms of "you derive from them and then probably have to implement
things").
How that difference are reflected on the derivated class ?
In my code, what if i change the interface in an abstract class ? what
change could i have to do ?

Sorry, I don't understand what you mean. Could you ask again, possibly
with an example?
I want to notice that i am pretty sure that in my case, I could not use
other than interface. I have some dynamic Dlls containing all the same
classes, but different implementation. I acces to that classes by
dynamically loading the assembly, found the classes, load the adapted
methods, and create an interface instance.

Do you think i just have to replace Interface by abstract class, add the
abstract new string ToString(); compile and all would be ok ?

Well, that depends on whether any classes which are currently
implementing the interface also derive from other classes. You can
implement many interfaces, but only derive from one class.
 
an abstract class is where you have PARTIAL implementation, an interface you
have NO implementation, thats up to the implementer.
 
Thank you both for your help. !!


Differences are a bit more clear.
But i have no more time to spent to review my structure, so i think i will
not change anything.
ROM
 
Back
Top