Richard Knop
Richard Knop

Reputation: 83725

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.

Upvotes: 5

Views: 21061

Answers (5)

Toffee
Toffee

Reputation: 4626

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();

Upvotes: 1

Owen
Owen

Reputation: 7098

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.

Upvotes: 6

Kent Boogaart
Kent Boogaart

Reputation: 178740

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

Upvotes: 5

Stefan Steinegger
Stefan Steinegger

Reputation: 64628

You need to

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

Example example = new Example();

Upvotes: 10

Mikko Rantanen
Mikko Rantanen

Reputation: 8084

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

Upvotes: 1

Related Questions