By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,115 Members | 894 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,115 IT Pros & Developers. It's quick & easy.

Derived class Interface method needs to call Base Class Interface Method

P: n/a
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( );
}
}
Dec 1 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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 Frey wrote:
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( );
}
}

Dec 1 '06 #2

P: n/a
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 Shooter wrote:
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 Frey wrote:
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( );
}
}
Dec 1 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.