Interface Inheritance

S

Sean Chambers

Not sure if this is the correct way to go about this, but it seems
correct. I'm trying to eliminate dependencies through interfaces and
want to minimize casting as much as possible, as a result I have
interface inheritance setup like so

Here's my class setup:

BlogPost : IBlogPost
PodcastPost : BlogPost, IPodcastPost

IPodcastPost : IBlogPost

Class Blog
- has reference to IBlogPost

Class Podcast
- has reference to IPodcastPost


Now, If I pass around an instance of PodcastPost to methods that accept
IBlogPost, will it require
a cast? This inheritance structure seems a little complex, but then on
the other hand in my unittests, it seems to be accomplishing everything
I need it to.

Is this too confusing?

thanks

Sean
 
B

Bruce Wood

Sean said:
Not sure if this is the correct way to go about this, but it seems
correct. I'm trying to eliminate dependencies through interfaces and
want to minimize casting as much as possible, as a result I have
interface inheritance setup like so

Here's my class setup:

BlogPost : IBlogPost
PodcastPost : BlogPost, IPodcastPost

IPodcastPost : IBlogPost

Class Blog
- has reference to IBlogPost

Class Podcast
- has reference to IPodcastPost


Now, If I pass around an instance of PodcastPost to methods that accept
IBlogPost, will it require
a cast? This inheritance structure seems a little complex, but then on
the other hand in my unittests, it seems to be accomplishing everything
I need it to.

No. No cast would be required.
 
S

Sean Chambers

Is it necessary for my IPodcastPost interface to inherit from the
IBlogPost interface?

I would imagine if PodcastPost inherits from BlogPost that it can also
be referenced as IBlogPost since BlogPost implements IBlogPost, but
this doesn't seem to be the case. Maybe I'm missing something here.

any "aha" moments are welcome =)

thanks again
 
B

Bruce Wood

Could you post a short but complete program that illustrates the
problem? That way we can see code and see what doesn't work.
 
S

Sean Chambers

Ok,

I think I am just confusing myself here

here is some code:

public class Blog {
public void AddPost(IBlogPost) {}
}

public class Podcast : Blog {}



public class BlogPost : IBlogPost {}

public class PodcastPost : IPodcastPost {}



IBlogPost {
string title {
get;
set;
}

//other properties/methods

}

IPodcastPost : IBlogPost {}


It works with the above code, it just doesnt feel correct, inheriting
from another interface, but I guess otherwise I wouldn't be able to
pass an instance of type PodcastPost into a method that accepts an
IBlogPost.

It works like this, I think i am just running myself in a circle.

Any comments? Does it look like it makes sense?

=P
 
J

Jeff Louie

Just factor out the common interface IPost

IPost {
string title {
get;
set;
}

AddPost(IPost)

BlogPost implements IPost
PodCastPost implements IPost

Regards,
Jeff
 
S

Sean Chambers

well,

IPodcastPost has an additional field called "Attachment"

interface IPodcastPost {
public string Attachment {
get;
set;
}
}

factoring both types to one interface would still force me to cast to
PodcastPost when I want to use the Attachment field.

I assume there is no way around this? That was why I also had the
IPodcastPost interface because it contains all the IBlogPost fields, as
well as the Attachment field.
 
S

Sean Chambers

What I decided to do is the following:

Make an Abstract base class called "Post", which both BlogPost and
PodcastPost derive from.

the abstract class implements IPost, so if the class needs to be
referenced outside of the current context I can use IPost, i also use
the interface when setting up relations between the Blog and BlogPost,
Podcast and PodcastPost classes.

This refactoring makes it much clearer to understand and makes it
easier to extend the class later down the road.

I think I was just looking too deeply into the factoring at hand. =)

thank you everyone!

Sean
 
J

Jeff Louie

Sean.... Just look at an interface as a purely abstract base class with
no
implementation details. The problem is you only get to inherit from a
single
base class so you don't want to waste it. You can inherit from more than
one
interface, however. If you really factor out the interfaces you might
have;

IPost
IAttachment

and a composite interface
IPodcastPost: IPost, IAttachment
or a class
PodcastPost: IPost, IAttachment

Then indeed you can pass a reference of type IPodcastPost or PodcastPost
to
a method that takes a parameter of type IPost.

AddPost(IPost) and AddPodcastPost(IPodcastPost) will then both take a
reference of type IPodcastPost and
AddPost(IPost) and AddPodcastPost(PodcastPost) will take a reference to
an
object of class Podcastpost.

Within the method AddPodcastPost you can call methods in IAttachment
without casting.

http://www.geocities.com/Jeff_Louie/OOP/oop16.htm

Regards,
Jeff
factoring both types to one interface would still force me to cast to
PodcastPost when I want to use the Attachment field.

I assume there is no way around this? That was why I also had the
IPodcastPost interface because it contains all the IBlogPost fields, as
well as the Attachment field.<
 
S

Sean Chambers

Jeff,

Thanks for the pointers,

On the project I am currently working on, I am really trying to make it
as loosly coupled as possible (which should always be the aim, but now
I am trying very hard to make this true)

In addition, I think I am getting too much tunnel vision given the
current requirements, for instance, I have no need at the current
moment in time to use a IAttachment interface, therefore I think it
would be premature to implement this interface since I have no need for
it.

I created an IPost interface and an Abstract class called Post, this
way I can extend Post, and all subclasses can be referenced as IPost.

Thanks for your help. just trying to wrap my head around this stuff.

sean
 

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