Abstract class must implement interfaces?

W

wesley

Hi,

Why must abstract classes implements all the methods/property in the
interface it implements? Since it's an abstract class it shouldn't be able
to be instantiated and the child classes are those that should implement the
interface. However in .Net I have to implement the interface in the abstract
class as well.

Why is this so?

Thanks
 
M

Mattias Sjögren

Why must abstract classes implements all the methods/property in the
interface it implements?

They don't, really. They just have to declare the method, not
necessarily provide an implementation. So the following works

interface IFoo
{
void Bar();
}

abstract class FooBaseImpl : IFoo
{
public abstract void Bar();
}

class ConcreteFoo : FooBaseImpl
{
public override void Bar() {}
}



Mattias
 
W

wesley

Why must abstract classes implements all the methods/property in the
They don't, really. They just have to declare the method, not
necessarily provide an implementation. So the following works

Yes correct, however why do they even need to declare the methods? Since
they are not defining the methods. It should be left to the concrete class
to implement those methods. Isn't this actually logical? What is the reason
that it is implemented that way?

Thanks,
 
A

Armin Zingler

dim i as IFoo
dim o as FooBaseImpl

i = o

This wouldn't work if FooBaseImpl didn't implement the interface.
 
W

wesley

dim i as IFoo
dim o as FooBaseImpl

i = o

This wouldn't work if FooBaseImpl didn't implement the interface.

Why wouldn't it work? Because if FooBaseImpl is abstract then there is no
need to implement the interface methods? Because it may not be relevant to
implement it there. If FooBaseImpl is abstract and declared that it
implements the IFoo interface the compiler should just leave it be and only
bind the methods at runtime and it doesn't need to check it at compiler
time. IMO

wes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top