Question about interface

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Quentin Huo

    Question about interface

    Hi:

    C# doesn't support multiple inheritance but it supports interface.But I
    think these ways are different.

    For example, C++ supports multiple inheritance. I have two classess classA
    and classB:

    class classA(){
    ......
    public void fly(){
    ......
    }
    ......
    }

    class classB(){
    ......
    public void swim(){
    ......
    }
    ......
    }

    Now we can create another class "classC" which inherits from "classA" and
    "classB".
    So classC will have the features of fly and swim. The important is we don't
    need do anything else.

    But in C#, it doesn't support multiple inheritance. Now, if I need to create
    a class "classC" and it will have the "fly" and "swim" functions in the
    existing classes "classA" and "classB", how can I do ?

    I think one solution is
    - create an interafce "interfaceS wim" and "swim" method;
    - classB inherits from classA and interfaceSwim, and implements the swim
    method;
    - create classC which inherits from classB.

    But there will be one more level of inheritance architecture. Is it correct?
    or better solution?

    Thanks

    Q.


  • Bob Grommes

    #2
    Re: Question about interface

    Actually I would just implement an abstract method, Travel(), and let *its*
    implementations in child classes decide whether to fly or swim.

    Multiple inheritance is one of those sexy ideas that degenerates into a
    quagmire of spaghetti very quickly. Interfaces let you implement a species
    of mixins which is IMO about all MI is good for anyway.

    If you really want MI, however, the Eiffel.NET language *does* provide it.
    It even provides generics without waiting for CLR 2.0.

    --Bob

    "Quentin Huo" <q.huo@manyworl ds.com> wrote in message
    news:%23WtNctjY FHA.2124@TK2MSF TNGP14.phx.gbl. ..[color=blue]
    > Hi:
    >
    > C# doesn't support multiple inheritance but it supports interface.But I
    > think these ways are different.
    >
    > For example, C++ supports multiple inheritance. I have two classess classA
    > and classB:
    >
    > class classA(){
    > ......
    > public void fly(){
    > ......
    > }
    > ......
    > }
    >
    > class classB(){
    > ......
    > public void swim(){
    > ......
    > }
    > ......
    > }
    >
    > Now we can create another class "classC" which inherits from "classA" and
    > "classB".
    > So classC will have the features of fly and swim. The important is we
    > don't need do anything else.
    >
    > But in C#, it doesn't support multiple inheritance. Now, if I need to
    > create a class "classC" and it will have the "fly" and "swim" functions in
    > the existing classes "classA" and "classB", how can I do ?
    >
    > I think one solution is
    > - create an interafce "interfaceS wim" and "swim" method;
    > - classB inherits from classA and interfaceSwim, and implements the swim
    > method;
    > - create classC which inherits from classB.
    >
    > But there will be one more level of inheritance architecture. Is it
    > correct? or better solution?
    >
    > Thanks
    >
    > Q.[/color]


    Comment

    • Bjorn Abelli

      #3
      Re: Question about interface


      "Quentin Huo" <q.huo@manyworl ds.com> skrev i meddelandet
      news:%23WtNctjY FHA.2124@TK2MSF TNGP14.phx.gbl. ..[color=blue]
      > Hi:
      >
      > C# doesn't support multiple inheritance but it supports interface.But I
      > think these ways are different.
      >
      > For example, C++ supports multiple inheritance. I have two classess classA
      > and classB:
      >
      > class classA(){
      > ......
      > public void fly(){
      > ......
      > }
      > ......
      > }
      >
      > class classB(){
      > ......
      > public void swim(){
      > ......
      > }
      > ......
      > }
      > Now we can create another class "classC" which inherits
      > from "classA" and "classB".
      > So classC will have the features of fly and swim. The
      > important is we don't need do anything else.
      >
      > But in C#, it doesn't support multiple inheritance. Now, if I need to
      > create a class "classC" and it will have the "fly" and "swim" functions in
      > the existing classes "classA" and "classB", how can I do ?[/color]

      One fairly common way is to use composition:


      interface IFlyer {
      void fly();
      }

      interface ISwimmer {
      void swim();
      }


      class ClassA : IFlyer {
      ......
      public void fly(){
      ......
      }
      ......
      }

      class ClassB : ISwimmer {
      ......
      public void swim(){
      ......
      }
      ......
      }

      class ClassC : IFlyer, ISwimmer {

      ......
      ClassA a;
      ClassB b;
      ......

      public void fly(){
      a.fly();
      }

      public void swim() {
      b.swim();
      }
      }

      // Bjorn A


      Comment

      • Nick Malik [Microsoft]

        #4
        Re: Question about interface

        One variation on this is to simply make the composed parts available, while
        hiding their construction. You lose substituability and you lose the
        ability for the descendent class to access private members, but you get a
        flexible interface that doesn't need to be changed when the parent types
        change.

        class ClassC {

        ......
        ClassA _a;
        ClassB _b;

        public ClassC {
        _a = new ClassA();
        _b = new ClassB();
        }
        ......

        public ClassA a {
        get { return _a;}
        }

        public ClassB b {
        get { return _b;}
        }
        }



        --
        --- Nick Malik [Microsoft]
        MCSD, CFPS, Certified Scrummaster


        Disclaimer: Opinions expressed in this forum are my own, and not
        representative of my employer.
        I do not answer questions on behalf of my employer. I'm just a
        programmer helping programmers.
        --
        "Bjorn Abelli" <bjorn_abelli@D oNotSpam.hotmai l.com> wrote in message
        news:%23ysYpskY FHA.1412@TK2MSF TNGP12.phx.gbl. ..[color=blue]
        >
        > "Quentin Huo" <q.huo@manyworl ds.com> skrev i meddelandet
        > news:%23WtNctjY FHA.2124@TK2MSF TNGP14.phx.gbl. ..[color=green]
        >> Hi:
        >>
        >> C# doesn't support multiple inheritance but it supports interface.But I
        >> think these ways are different.
        >>
        >> For example, C++ supports multiple inheritance. I have two classess
        >> classA and classB:
        >>
        >> class classA(){
        >> ......
        >> public void fly(){
        >> ......
        >> }
        >> ......
        >> }
        >>
        >> class classB(){
        >> ......
        >> public void swim(){
        >> ......
        >> }
        >> ......
        >> }
        >> Now we can create another class "classC" which inherits
        >> from "classA" and "classB".
        >> So classC will have the features of fly and swim. The
        >> important is we don't need do anything else.
        >>
        >> But in C#, it doesn't support multiple inheritance. Now, if I need to
        >> create a class "classC" and it will have the "fly" and "swim" functions
        >> in the existing classes "classA" and "classB", how can I do ?[/color]
        >
        > One fairly common way is to use composition:
        >
        >
        > interface IFlyer {
        > void fly();
        > }
        >
        > interface ISwimmer {
        > void swim();
        > }
        >
        >
        > class ClassA : IFlyer {
        > ......
        > public void fly(){
        > ......
        > }
        > ......
        > }
        >
        > class ClassB : ISwimmer {
        > ......
        > public void swim(){
        > ......
        > }
        > ......
        > }
        >
        > class ClassC : IFlyer, ISwimmer {
        >
        > ......
        > ClassA a;
        > ClassB b;
        > ......
        >
        > public void fly(){
        > a.fly();
        > }
        >
        > public void swim() {
        > b.swim();
        > }
        > }
        >
        > // Bjorn A
        >
        >[/color]


        Comment

        Working...