Interface Inheritance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sean Chambers

    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

  • Bruce Wood

    #2
    Re: Interface Inheritance


    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.

    Comment

    • Sean Chambers

      #3
      Re: Interface Inheritance

      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.

      Comment

      • Bruce Wood

        #4
        Re: Interface Inheritance

        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.

        Comment

        • Sean Chambers

          #5
          Re: Interface Inheritance

          Ok,

          I think I am just confusing myself here

          here is some code:

          public class Blog {
          public void AddPost(IBlogPo st) {}
          }

          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.

          Comment

          • Jeff Louie

            #6
            Re: Interface Inheritance

            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 ***

            Comment

            • Sean Chambers

              #7
              Re: Interface Inheritance

              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 ***

              Comment

              • Sean Chambers

                #8
                Re: Interface Inheritance

                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 ***

                Comment

                • Jeff Louie

                  #9
                  Re: Interface Inheritance

                  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.

                  Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


                  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 ***

                  Comment

                  • Sean Chambers

                    #10
                    Re: Interface Inheritance

                    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.
                    >
                    Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!

                    >
                    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 ***

                    Comment

                    Working...