abstract class inheritance

C

Claire

I have a base abstract class A which contains several abstract functions

I then have an abstract class B (derived from A) which overrides a small
subset of A's abstract functions

Finally I have a fully functional class C (deriving from B). Class C
overrides the (A's) abstract functions not tackled by its ancestor B.

I am complained at by the compiler, that in C, I have not implemented the
inherited abstract members defined by A. But I overrode these functions in
B!

Is this correct and why?

Claire

public abstract class A
{
public abstract void Function1();
public abstract void Function2();
public abstract void Function3();
}

public abstract class B : A
{
public override void Function1()
{
doodar;
}
}

public class C : B
{
public override void Function2()
{
}
public override void Function3()
{
}
// Compiler complains that C has not implemented Function1
}
 
D

Dan

Claire,

You have a typo somewhere in your code, or the problem is somewhere else. I
simply cut and pasted your example code below into a new class file, and
created

A eg = new C();

and it compiled fine. (c# with .Net 1.1)

Daniel.
 
J

Joerg Jooss

Claire said:
I have a base abstract class A which contains several abstract
functions
I then have an abstract class B (derived from A) which overrides a
small subset of A's abstract functions

Finally I have a fully functional class C (deriving from B). Class C
overrides the (A's) abstract functions not tackled by its ancestor B.

I am complained at by the compiler, that in C, I have not implemented
the inherited abstract members defined by A. But I overrode these
functions in B!

Is this correct and why?

Claire

public abstract class A
{
public abstract void Function1();
public abstract void Function2();
public abstract void Function3();
}

public abstract class B : A
{
public override void Function1()
{
doodar;
}
}

public class C : B
{
public override void Function2()
{
}
public override void Function3()
{
}
// Compiler complains that C has not implemented Function1
}

It should compile. And it actually does for me (.NET 1.1, VS .NET 2003).

Cheers,
 
T

Tom Porterfield

Claire said:
I have a base abstract class A which contains several abstract functions

I then have an abstract class B (derived from A) which overrides a small
subset of A's abstract functions

Finally I have a fully functional class C (deriving from B). Class C
overrides the (A's) abstract functions not tackled by its ancestor B.

I am complained at by the compiler, that in C, I have not implemented the
inherited abstract members defined by A. But I overrode these functions in
B!

Is this correct and why?

Claire

public abstract class A
{
public abstract void Function1();
public abstract void Function2();
public abstract void Function3();
}

public abstract class B : A
{
public override void Function1()
{
doodar;
}
}

public class C : B
{
public override void Function2()
{
}
public override void Function3()
{
}
// Compiler complains that C has not implemented Function1
}

Verify that you have everything correct. The code you have above
compiles fine (after commeting out doodar as that doesn't exist) as it
should. But this appears to be just sample code, not the real stuff.
Is the error about not implementing Function1 in C the only error you
are getting?
 
R

Richard Blewett

did you miss out the override keyword on one of the method implementations
in B in the real code.

In that case the C# compiler would assume that the new method is unrelated
to the abstract one (as it didn't deliberately use the override keyword)

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
 

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