Derived class Interface method needs to call Base Class Interface Method


K

Kevin Frey

In a derived class I am trying to redefine the implementation of a interface
method defined in a base class - the base class interface method was not
declared virtual.

I have yet to actually test this approach, but it looks ugly. Is there any
better way?

public interface ITest
{
void Func();
}

public class A : ITest
{
void ITest.Func( )
{
// some implementation
}
}

public class B : A, ITest
{
void ITest.Func( )
{
// want to call A's ITest interface
( ( ITest )( A ) this ).Func( );
}
}
 
Ad

Advertisements

D

Dave Shooter

As class B extends class A, and class A has already implemented ITest,
you do not need to implement it again in B. You can simply call B.Func()
as follows:

public interface ITest
{
void Func();
}

public class A : ITest
{
// Implement ITest.Func() methods
public void Func()
{
Console.Out.WriteLine("In A.Func()...");
}
}

public class B : A
{
}

class Program
{
static void Main(string[] args)
{
B b = new B();
b.Func(); // Prints In A.Func()...
}
}

If I've misunderstood, and you actually want to redefine the
implementation of the non-virtual Func() method in class B, then you can
hide A's implementation using the 'new' keyword:

public new void Func()
{
Console.Out.WriteLine("In B.Func()...");
}

Regards
Dave
 
Ad

Advertisements

B

Bruce Wood

True, but beware that if you use the "new" modifier then a caller can
cast a derived-class object back to the base class and access the base
class implementation, thus:

B bObject = new B();
A aObject = bObject;
aObject.Func();

will invoke A.Func(), not B.Func().

If A has not declared Func() virtual then this is the best you can do,
unfortunately.

Dave said:
As class B extends class A, and class A has already implemented ITest,
you do not need to implement it again in B. You can simply call B.Func()
as follows:

public interface ITest
{
void Func();
}

public class A : ITest
{
// Implement ITest.Func() methods
public void Func()
{
Console.Out.WriteLine("In A.Func()...");
}
}

public class B : A
{
}

class Program
{
static void Main(string[] args)
{
B b = new B();
b.Func(); // Prints In A.Func()...
}
}

If I've misunderstood, and you actually want to redefine the
implementation of the non-virtual Func() method in class B, then you can
hide A's implementation using the 'new' keyword:

public new void Func()
{
Console.Out.WriteLine("In B.Func()...");
}

Regards
Dave

Kevin said:
In a derived class I am trying to redefine the implementation of a interface
method defined in a base class - the base class interface method was not
declared virtual.

I have yet to actually test this approach, but it looks ugly. Is there any
better way?

public interface ITest
{
void Func();
}

public class A : ITest
{
void ITest.Func( )
{
// some implementation
}
}

public class B : A, ITest
{
void ITest.Func( )
{
// want to call A's ITest interface
( ( ITest )( A ) this ).Func( );
}
}
 

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