Abstract class must implement interfaces?

  • Thread starter Thread starter wesley
  • Start date Start date
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
 
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
 
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,
 
dim i as IFoo
dim o as FooBaseImpl

i = o

This wouldn't work if FooBaseImpl didn't implement the interface.
 
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
 
Back
Top