简体   繁体   中英

Class both extends an abstract class and implements an interface

What if I have a class that both extends an abstract class and implements an interface, for example:

class Example : AbstractExample, ExampleInterface
{
    // class content here
}

How can I initialize this class so I can access methods from both the interface and the abstract class?

When I do:

AbstractExample example = new Example();

I cannot access methods from the interface.

You need to

  • implement the interface in AbstractExample
  • or get a reference to Example

Example example = new Example();

The last example will tie you to a solid instance of either the interface or abstract class which I presume is not your goal.The bad news is you're NOT in a dynamically typed language here, so your stuck with either having a reference to a solid "Example" objects as previously sprcified or casting/uncasting ie:

AbstractExample example = new Example();
((IExampleInterface)example).DoSomeMethodDefinedInInterface();

Your other alternitives are to have both AbstractExample and IExampleInterface implement a common interface so you would have ie

abstract class AbstractExample : ICommonInterface
interface IExampleInterface : ICommonInterface
class Example : AbstractExample, IExampleInterface

Now you could work with ICommonInterface and have the functionality of both the abstract class and the implementation of your IExample interface.

If none of these answers are acceptable, you may want to look at some of the DLR languages that run under the .NET framework ie IronPython.

If you only know the abstract class, it suggests that you know the actual type via an instance of Type . Therefore, you could use generics:

private T SomeMethod<T>()
    where T : new(), AbstractExample, ExampleInterface
{
    T instance = new T();
    instance.SomeMethodOnAbstractClass();
    instance.SomeMethodOnInterface();
    return instance;
}

I think this example will help you:

public interface ICrud
{
    void Add();
    void Update();
    void Delete();
    void Select();
}

public abstract class CrudBase
{
    public void Add()
    {
        Console.WriteLine("Performing add operation...");
        Console.ReadLine();
    }

   public void Update()
    {
        Console.WriteLine("Performing update operation...");
        Console.ReadLine();
    }
    public void Delete()
    {
        Console.WriteLine("Performing delete operation...");
        Console.ReadLine();
    }
    public void Select()
    {
        Console.WriteLine("Performing select operation...");
        Console.ReadLine();
    }
}

public class ProcessData : CrudBase, ICrud
{

}

var process = new ProcessData();
process.Add();

Use:

Example example = new Example();

Updated after more information:

If you are sure it implements ExampleInterface, you can use

AbstractClass example = new Example();
ExampleInterface exampleInterface = (ExampleInterface)example;
exampleInterface.InterfaceMethod();

You can also make sure it really implements it by checking the interface with

if (example is ExampleInterface) {
    // Cast to ExampleInterface like above and call its methods.
}

I don't believe Generics help you as those are resolved compile time and if you only have a reference to the AbstractClass the compiler will complain.

Edit: So more or less what Owen said. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM