implement interface

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Johansson

    implement interface

    Hello!

    Assume you have the following interface and classes shown below.

    It is said that a class must implement all the methods in the interface it
    inherits.
    Below we have class MyDerivedClass that inherits IMyInterface but
    MyDerivedClass doesn't implement method DoSomething() it inherits it from
    the base class MyBaseClass.
    So the statement that a class must implement all method in an interface that
    it inherits is not fully true as I see this matter.
    Do you agree with me ?

    public interface IMyInterface
    {
    void DoSomething();
    void DoSomethingElse ();
    }

    public class MyBaseClass
    {
    public void DoSomething() { }
    }

    public class MyDerivedClass : MyBaseClass, IMyInterface
    {
    public void DoSomethingElse () {}
    }

    //Tony


  • =?ISO-8859-1?Q?G=F6ran_Andersson?=

    #2
    Re: implement interface

    Tony Johansson wrote:
    Hello!
    >
    Assume you have the following interface and classes shown below.
    >
    It is said that a class must implement all the methods in the interface it
    inherits.
    Below we have class MyDerivedClass that inherits IMyInterface but
    MyDerivedClass doesn't implement method DoSomething() it inherits it from
    the base class MyBaseClass.
    So the statement that a class must implement all method in an interface that
    it inherits is not fully true as I see this matter.
    Do you agree with me ?
    >
    public interface IMyInterface
    {
    void DoSomething();
    void DoSomethingElse ();
    }
    >
    public class MyBaseClass
    {
    public void DoSomething() { }
    }
    >
    public class MyDerivedClass : MyBaseClass, IMyInterface
    {
    public void DoSomethingElse () {}
    }
    >
    //Tony
    >
    The person writing that description probably didn't think of that
    special case, or throught that it was more important to keep the
    description simple, than to make it cover every possible aspect.


    --
    Göran Andersson
    _____
    Göran Anderssons privata hemsida.

    Comment

    • Scott M.

      #3
      Re: implement interface

      It is said that a class must implement all the methods in the interface it
      inherits.
      Well no. A class must implement all the methods of the interface it
      "implements ". Classes do not "inherit" interfaces. Implementation and
      inheritance are not the same thing and thus, the source of your confusion.

      MyDerivedClass doesn't have to implement DoSomething() because it is
      inheriting the implementation of DoSomething() from the base class. It does
      have to "implement" DoSomethingElse () because the class "implements " the
      interface.

      So, when a class "inherits" from a base class, it inherits all that the base
      class has. When a class "implements " an interface it is entering into a
      contract that requires that the entire interface be implemented.

      -Scott



      "Tony Johansson" <johansson.ande rsson@telia.com wrote in message
      news:jhj2k.8132 $R_4.6512@newsb .telia.net...
      Hello!
      >
      Assume you have the following interface and classes shown below.
      >
      It is said that a class must implement all the methods in the interface it
      inherits.
      Below we have class MyDerivedClass that inherits IMyInterface but
      MyDerivedClass doesn't implement method DoSomething() it inherits it from
      the base class MyBaseClass.
      So the statement that a class must implement all method in an interface
      that it inherits is not fully true as I see this matter.
      Do you agree with me ?
      >
      public interface IMyInterface
      {
      void DoSomething();
      void DoSomethingElse ();
      }
      >
      public class MyBaseClass
      {
      public void DoSomething() { }
      }
      >
      public class MyDerivedClass : MyBaseClass, IMyInterface
      {
      public void DoSomethingElse () {}
      }
      >
      //Tony
      >

      Comment

      • Peter Duniho

        #4
        Re: implement interface

        On Fri, 06 Jun 2008 15:57:19 -0700, Tony Johansson
        <johansson.ande rsson@telia.com wrote:
        Hello!
        >
        Assume you have the following interface and classes shown below.
        >
        It is said that a class must implement all the methods in the interface
        it
        inherits.
        It is said by whom? How do they define the term "implements "?

        As Scott said, interfaces are _implemented_, not inherited. But even
        ignoring that mistake in your terminology, it's perfectly reasonable to
        say that a class that inherits some implementation does in fact in some
        sense implement both the inherited implementation and new implementation.

        Anyway, it's really hard to say one way or the other whether a specific
        statement is accurate or not without you actually posting the statement
        and the full context in which it was made.

        Pete

        Comment

        • Hans Kesting

          #5
          Re: implement interface

          Tony Johansson expressed precisely :
          Hello!
          >
          It is said that a class must implement all the methods in the interface it
          inherits.
          I think it depends on what you mean by "implement a method".

          If it is strict "must physically define this method within this class",
          then the above statement is false.

          If it is the more broad "an instance of this class should have this
          method, either directly or through inheritance", then the statement is
          true. And your example would not violate it.

          Hans Kesting


          Comment

          • Scott M.

            #6
            Re: implement interface

            I think you're confusing things for the OP.

            There really isn't (or shouldn't be) a dual meaning for "implements ". A
            method is implemented when a class provides functionality that the interface
            requires and that functionality matches the name/signature that the
            interface requires.

            If a class inherits from a class that implements a method, the derived class
            does not implement the method, but instead has an implementation of the
            method.

            In other words, implementation occurs at the level where the method is
            provided to the class.

            -Scott

            "Hans Kesting" <news.hansdk@sp amgourmet.comwr ote in message
            news:OqnmWTgyIH A.5892@TK2MSFTN GP02.phx.gbl...
            Tony Johansson expressed precisely :
            >Hello!
            >>
            >It is said that a class must implement all the methods in the interface
            >it inherits.
            >
            I think it depends on what you mean by "implement a method".
            >
            If it is strict "must physically define this method within this class",
            then the above statement is false.
            >
            If it is the more broad "an instance of this class should have this
            method, either directly or through inheritance", then the statement is
            true. And your example would not violate it.
            >
            Hans Kesting
            >
            >

            Comment

            Working...