473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Interface Inheritance

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

Oct 19 '06 #1
9 1471

Sean Chambers wrote:
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.

Oct 19 '06 #2
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

Bruce Wood wrote:
Sean Chambers wrote:
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.
Oct 19 '06 #3
Could you post a short but complete program that illustrates the
problem? That way we can see code and see what doesn't work.

Sean Chambers wrote:
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

Bruce Wood wrote:
Sean Chambers wrote:
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.
Oct 19 '06 #4
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
Bruce Wood wrote:
Could you post a short but complete program that illustrates the
problem? That way we can see code and see what doesn't work.

Sean Chambers wrote:
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

Bruce Wood wrote:
Sean Chambers wrote:
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.
Oct 19 '06 #5
Just factor out the common interface IPost

IPost {
string title {
get;
set;
}

AddPost(IPost)

BlogPost implements IPost
PodCastPost implements IPost

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Oct 20 '06 #6
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.
Jeff Louie wrote:
Just factor out the common interface IPost

IPost {
string title {
get;
set;
}

AddPost(IPost)

BlogPost implements IPost
PodCastPost implements IPost

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Oct 20 '06 #7
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

Sean Chambers wrote:
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.
Jeff Louie wrote:
Just factor out the common interface IPost

IPost {
string title {
get;
set;
}

AddPost(IPost)

BlogPost implements IPost
PodCastPost implements IPost

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Oct 20 '06 #8
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.<

*** Sent via Developersdex http://www.developersdex.com ***
Oct 20 '06 #9
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
Jeff Louie wrote:
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.<

*** Sent via Developersdex http://www.developersdex.com ***
Oct 20 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Tom Evans | last post by:
My basic question: If I have a specific interface which I know is going to be implemented by a number of classes, but there is no implementation commonality between them, what is the preferred...
4
by: Roy Pereira | last post by:
I have an application that is composed of a set of "Content" dlls and a viewer application. The viewer calls a standard set of functions that are present in all the dlls. I maintain this by...
4
by: christopher diggins | last post by:
A feature that I find signficantly missing in C# is the ability to write functions in interfaces that can call other functions of the interface. Given an interface ISomeInteface the only way we can...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
7
by: Hazz | last post by:
Are there any good references/articles/books which provide clarity toward my insecurity still on deciding how to model a complex system? I still feel uncomfortable with my understanding, even...
10
by: Brett | last post by:
I'm still trying to figure out concrete reasons to use one over the other. I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract...
6
by: John Salerno | last post by:
I understand how they work (basically), but I think maybe the examples I'm reading are too elementary to really show their value. Here's one from Programming C#: #region Using directives ...
12
by: Meya-awe | last post by:
I am puzzled, what is the purpose of an interface? How does it work, what i mean is how does the compiler treats this? Why when we talk about separating user interface from business logic, an...
4
by: Raja Chandrasekaran | last post by:
Hai friends, I really wonder, If the interface does not have any definition, Y do we need to use interface. You can then only we can use Multiple inheritance. I really cant understand, Just for...
52
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.