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

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.
 
J

Joanna Carter [TeamB]

"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
 
T

Tom Spink

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top