Partial implementation of interface

A

Advait Mohan Raut

Hello friends,
I am new to C#. I have some fundamental doubt. Please, try to answer
it.

1) Ltes say, IFace is an interface which has proto of two methods.
class A implements IFase.

interface IFace
{
void func1(...) {.....}
void func2(...) {.....}
}

class A : IFace
{
void func1(...) {.....}
void func2(...) {.....}
}

class B : A, IFace
{
....
}

Now the question is, in the defination of class B is it neccesory to
implement all two members of IFace. I dont want to change defination
of func1( ) which is from A to B. Or implementation of only one will
solve the problem ?
 
N

Nicholas Paldino [.NET/C# MVP]

Advait,

Class B, if it does not define anything else, will implement the
interface IFace (you shouldn't need it in the declaration of B, as a matter
of fact).

If you want to provide an implementation of one of the methods defined
in IFace, then you will need to declare the methods in A as virtual, so that
B can override them.
 
A

Advait Mohan Raut

Advait,

Class B, if it does not define anything else, will implement the
interface IFace (you shouldn't need it in the declaration of B, as a matter
of fact).

If you want to provide an implementation of one of the methods defined
in IFace, then you will need to declare the methods in A as virtual, so that
B can override them.

But is it possible that, a method delared in an interface to be
implemented as virtual ?
 
N

Nicholas Paldino [.NET/C# MVP]

Advait,

By definition, the methods in the interface are virtual.

However, the concern here is the implementation. In your base class,
the interface is implicitly implemented, meaning that the public methods
which match the signatures are used for the interface implementation. It's
because of this that if you define the method as virtual, you can override
it in derived classes and have it work, if you wish.

If the interface is explicitly implemented, then you will have to
redefine the complete interface implementation in the derived classes. You
can't selectively override just a single member of the interface (last I
checked). In order to get around this, in the base class implementation, I
create the explicit interface implementation and then call protected virtual
methods which can be overriden in derived classes.
 
A

Advait Mohan Raut

Advait,

By definition, the methods in the interface are virtual.

However, the concern here is the implementation. In your base class,
the interface is implicitly implemented, meaning that the public methods
which match the signatures are used for the interface implementation. It's
because of this that if you define the method as virtual, you can override
it in derived classes and have it work, if you wish.

If the interface is explicitly implemented, then you will have to
redefine the complete interface implementation in the derived classes. You
can't selectively override just a single member of the interface (last I
checked). In order to get around this, in the base class implementation, I
create the explicit interface implementation and then call protected virtual
methods which can be overriden in derived classes.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

But is it possible that, a method delared in an interface to be
implemented as virtual ?


Than you, Nicholas Paldino for the nice information. Actuly I came to
conclusion the one cannot override selected methods of the interface,
because in the offline help of VS 2005 (i.e. C# Reference) they have
stated that 'override' will work only if the base class proto is
either 'override' or 'abstract' or 'virtual' .
 
J

Jon Skeet [C# MVP]

Advait Mohan Raut said:
Than you, Nicholas Paldino for the nice information. Actuly I came to
conclusion the one cannot override selected methods of the interface,
because in the offline help of VS 2005 (i.e. C# Reference) they have
stated that 'override' will work only if the base class proto is
either 'override' or 'abstract' or 'virtual' .

But when you implement it in the base class you can make it virtual or
abstract. If you do that, you can override it in the derived class.
 

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