Class both extends an abstract class and implements an interface

19,426

Solution 1

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 i.e:

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 i.e.

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 i.e. IronPython.

Solution 2

You need to

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

Example example = new Example();

Solution 3

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;
}

Solution 4

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. :)

Solution 5

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();
Share:
19,426
Richard Knop
Author by

Richard Knop

I'm a software engineer mostly working on backend from 2011. I have used various languages but has been mostly been writing Go code since 2014. In addition, I have been involved in lot of infra work and have experience with various public cloud platforms, Kubernetes, Terraform etc. For databases I have used lot of Postgres and MySQL but also Redis and other key value or document databases. Check some of my open source projects: https://github.com/RichardKnop/machinery https://github.com/RichardKnop/go-oauth2-server https://github.com/RichardKnop

Updated on June 04, 2022

Comments

  • Richard Knop
    Richard Knop almost 2 years

    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.