views:

2348

answers:

6

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.

Richard

+1  A: 

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

Mikko Rantanen
Thank you.But what if I don't know the reference to the class, I know only which abstract class it extends and which interface it implements?Richard
Richard Knop
+6  A: 

You need to

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

Example example = new Example();

Stefan Steinegger
+3  A: 

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

HTH, Kent

Kent Boogaart
Really like this answer, and now you have my mind racing on how to take this solution further.
Owen
+2  A: 

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

Owen
Thank you both of your answers are very useful ;)I'm a little confused with C# because I come from PHP/Zend and it's pretty different language.
Richard Knop
Yeah, i think a lot of people came from that background too (me included). You can do things with static languages that aren't possible with dynamic and visa versa. The best thing to do is to understand the difference and see where the line can be blured slightly. .NET 3.5 introduced some great features that make the language "dynamicish", Kent Boogaart's solution is a good start to maybe get the features your looking for without the verbosity of extra interfaces.
Owen
Downvoted. C# is not a dynamically typed language. Also you do not provide the simplest answer, which is Example example = new Example();
Paul Batum
Yeah, C# is most certainly *not* a dynamically typed language.
Noldorin
Owen
@Owen: upvoted. This helps immensely. And, sometimes, people can be too quick on the downvote. The context of the sentence should've given a clue that there was a typo. :)
dboarman
A: 

so what is the difference between

Example example = new Example();

and

AbstractClass example = new Example();

it doesent seem to be covered in any of my books!

(I assume you mean "AbstractExample example = new Example();" for the second one.) The difference is: even though you have a reference to a concrete example object, you won't be able to access any of its members, only the members defined in the AbstractExample class. Try it out. This is a key part of polymorphism: http://digg.com/programming/OOP_-_Polymorphism_Explained_in_Plain_English_That_Everyone_Can_Understand!
Robert Venables
A: 

thanks its clear now, very penetrating..