The class extends the abstract class and implements the interface

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

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

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

When I do this:

 AbstractExample example = new Example(); 

I cannot access the methods from the interface.

+4
source share
5 answers

The final example will bind you to a solid instance of either an interface or an abstract class, which, I believe, is not your goal. The bad news is that you are not a dynamically typed language, so you are stuck either with a link to solid "Example" objects, as previously set or cast / unwanted, i.e.:

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

Your other alternatives should have both AbstractExample and IExampleInterface implement a common interface so you can: i.e.

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

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

If none of these answers is acceptable, you might want to take a look at some of the DLR languages ​​that run under the .NET platform, i.e. IronPython.

+5
source

You need

  • implement an interface in AbstractExample
  • or get a link to an example

Example example = new Example();

+10
source

If you know only an abstract class, this assumes that you know the actual type through an instance of Type . Therefore you can use generics:

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

Using:

 Example example = new Example(); 

Updated after receiving 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 that it really implements it by checking the interface with

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

I do not believe that Generics will help you because they allow compilation time, and if you only have a link to AbstractClass, then the compiler will complain.

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

+1
source

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(); 
+1
source

All Articles