Ok, I have a couple abstract classes and one interface. My question is pretty simple: is it possible to have an abstract class inherit an interface but not implement the methods, but instead have its child classes that inherit from it, implement
the interface methods?
E.g.
<code>
abstract class A : B
{
}
interface B
{
void Do();
}
abstract class C : A
{
}
public class D : C
{
Do()
{
Stuff();
}
}
public class E : C
{
Do()
{
Stuff();
}
}
</code>
Where classes D and E are inheriting from C which inherits from A which inherits the interface B, but B's method "Do()" is implemented in the children classes? Is this possible or do i have to have all child classes (D and E) inherit from the interface individually?
I know i could use abstract methods in the main class and do it this way, but i'd like to use an interface.
Yes, that should work fine - as long as the class is marked abstract then it should not force the concrete implementation of any of the methods defined in the interfaces that it implements. However, the first concrete (non-abstract) class that inherits from
the abstract class will be forced to define all the methods required that haven't been implemented in any superclass.
Have you tried to implement the Do method as an abstract method? In otherwords, have A implement the interface B and change the Do method to be abstract. I have never tried this, but I don't see why that wouldn't be possible.
Son of a gun, you're right, as my test project didn't compile. I must have been thinking about interface alone which you can do something like that. Say, interface A inherits/implements interface B. Interface B does not
have to explicitly declare the members in interface A. But you're right that the class implementing A does....
[code]
public interface A
{
void DoIt();
}
public interface B : A
{
void DoItAgain();
}
public abstract class C : B
{
public abstract DoIt();
public abstract DoItAgain();
}
[/code]
chapel21
Contributor
2485 Points
499 Posts
Abstract classes and Interfaces
Oct 21, 2005 03:19 PM|LINK
E.g.
<code>
abstract class A : B
{
}
interface B
{
void Do();
}
abstract class C : A
{
}
public class D : C
{
Do()
{
Stuff();
}
}
public class E : C
{
Do()
{
Stuff();
}
}
</code>
Where classes D and E are inheriting from C which inherits from A which inherits the interface B, but B's method "Do()" is implemented in the children classes? Is this possible or do i have to have all child classes (D and E) inherit from the interface individually? I know i could use abstract methods in the main class and do it this way, but i'd like to use an interface.
Thanks.
rsmoke21
Contributor
3931 Points
792 Posts
Re: Abstract classes and Interfaces
Oct 21, 2005 05:10 PM|LINK
Syc0F3ar
Participant
1105 Points
236 Posts
Re: Abstract classes and Interfaces
Oct 21, 2005 05:12 PM|LINK
chapel21
Contributor
2485 Points
499 Posts
Re: Abstract classes and Interfaces
Oct 21, 2005 06:25 PM|LINK
The solution is:
interface B
{
void Do();
}
abstract class A : B
{
public abstract void Do();
}
This combines abstraction with implementation so that you can still call the methods from a generalized type.
rsmoke21
Contributor
3931 Points
792 Posts
Re: Abstract classes and Interfaces
Oct 21, 2005 07:34 PM|LINK
Son of a gun, you're right, as my test project didn't compile. I must have been thinking about interface alone which you can do something like that. Say, interface A inherits/implements interface B. Interface B does not have to explicitly declare the members in interface A. But you're right that the class implementing A does....
[code]
public interface A
{
void DoIt();
}
public interface B : A
{
void DoItAgain();
}
public abstract class C : B
{
public abstract DoIt();
public abstract DoItAgain();
}
[/code]