is virtual necessary when inheriting from an abstract class thatimpmlements an interface?

  • Thread starter Thread starter Dan Holmes
  • Start date Start date
D

Dan Holmes

Suppose i have this class declaration:

public abstract class ConfigurableComponent : Component, IConfigure

if IConfigure has a method with this signature:

List<Parameter> ListParameters(Identity identity)

Would i need to implement it in the abstract class with as virtual in
order for it to be overrideable in a descendant class?

public virtual List<Parameter> ListParameters(Identity identity)

dan

Where in the docs would this kind of stuff live? I don't know how to
find this kind of theory.
 
"Dan Holmes" <[email protected]> a écrit dans le message de (e-mail address removed)...

| Suppose i have this class declaration:
|
| public abstract class ConfigurableComponent : Component, IConfigure
|
| if IConfigure has a method with this signature:
|
| List<Parameter> ListParameters(Identity identity)
|
| Would i need to implement it in the abstract class with as virtual in
| order for it to be overrideable in a descendant class?
|
| public virtual List<Parameter> ListParameters(Identity identity)

You can either implement interface members implicitly or explicitly :

implicit

public virtual List<Parameter> ListParameters(Identity identity)
{
...
}

explicit

protected virtual List<Parameter> ListParameters(Identity identity)
{
...
}

List<Parameter> IConfigure.ListParameters(Identity identity)
{
return ListParameters(identity);
}

Joanna
 
Dan said:
Suppose i have this class declaration:

public abstract class ConfigurableComponent : Component, IConfigure

if IConfigure has a method with this signature:

List<Parameter> ListParameters(Identity identity)

Would i need to implement it in the abstract class with as virtual in
order for it to be overrideable in a descendant class?

public virtual List<Parameter> ListParameters(Identity identity)

dan

Where in the docs would this kind of stuff live? I don't know how to
find this kind of theory.

If you want your method to be overridable in derived classes, then yes, you
must apply the 'virtual' modifier to the method declaration. If you want
to force derived classes to override the method, then apply the 'abstract'
modifier to the method declaration.

The interface is implemented on the class to which you specify in the class
declaration, i.e. on the class ConfigurableComponent in your case. Derived
classes in order to override your implementation need to be given the
virtual method.
 
Back
Top