Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Force Interface Implementation in Derived Classes?

Force Interface Implementation in Derived Classes?

Scheduled Pinned Locked Moved C#
helpbusinessperformancetutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pr1mem0ver
    wrote on last edited by
    #1

    Perhaps I want something that is impossible... but thought I would ask anyway because I cannot find a way to do this... and it seems like it should be possible. I want to force the definition of a method in all subclasses without that method being virtual. (I don't want a virtual table and lookup to be necessary). Normally one would do this with an interface. The problem is that I also need to define a property with an explicit definition. Normally that is done with an abstract class (which is the only reason I need to define one) An example is illustrated below. The problem I am running into is that if I inherit the interface in the abstract class, none of the sub-classes require the "NeededFunction" to be implemented, which is counter to what I want. If I don't implement the method in the abstract class however, it gives me an error. Is there a way to do this... basically I want the derived classes to implement the "NeededFunction" rather than the abstract class (without them being virtual for performance reasons). I know I can have every derived class implement the interface instead but that seems needlessly redundant.

    interface IMyFunctionInterface
    {
    void NeededFunction(object state);
    }

    internal abstract class MyBaseImplementation : IMyFunctionInterface
    {
    internal MyRelevantProperty { get; protected set; }
    public void NeededFunction(object state)
    {
    //defined here only to satisfy interface requirements...
    //want to force this to be implemented in each subclass rather than here
    }
    }

    internal class Implementation1 : MyBaseImplementation
    {
    public void NeededFunction(object state)... // want to define this here instead of in base class
    }

    Richard DeemingR L 2 Replies Last reply
    0
    • P pr1mem0ver

      Perhaps I want something that is impossible... but thought I would ask anyway because I cannot find a way to do this... and it seems like it should be possible. I want to force the definition of a method in all subclasses without that method being virtual. (I don't want a virtual table and lookup to be necessary). Normally one would do this with an interface. The problem is that I also need to define a property with an explicit definition. Normally that is done with an abstract class (which is the only reason I need to define one) An example is illustrated below. The problem I am running into is that if I inherit the interface in the abstract class, none of the sub-classes require the "NeededFunction" to be implemented, which is counter to what I want. If I don't implement the method in the abstract class however, it gives me an error. Is there a way to do this... basically I want the derived classes to implement the "NeededFunction" rather than the abstract class (without them being virtual for performance reasons). I know I can have every derived class implement the interface instead but that seems needlessly redundant.

      interface IMyFunctionInterface
      {
      void NeededFunction(object state);
      }

      internal abstract class MyBaseImplementation : IMyFunctionInterface
      {
      internal MyRelevantProperty { get; protected set; }
      public void NeededFunction(object state)
      {
      //defined here only to satisfy interface requirements...
      //want to force this to be implemented in each subclass rather than here
      }
      }

      internal class Implementation1 : MyBaseImplementation
      {
      public void NeededFunction(object state)... // want to define this here instead of in base class
      }

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      AFAIK, the only way to force the derived class to implement the method is to mark it as abstract. However, that would still be a virtual method.

      internal abstract class MyBaseImplementation : IMyFunctionInterface
      {
      internal Foo MyRelevantProperty { get; protected set; }
      public abstract void NeededFunction(object state);
      }

      internal class Implementation1: MyBaseImplementation
      {
      public sealed override void NeededFunction(object state) { ... }
      }

      If your base class doesn't include an interface member, then it doesn't implement the interface. All of the derived classes would need to implement the interface instead.

      pr1mem0ver wrote:

      without them being virtual for performance reasons

      Do you have any real performance data from your application to justify that requirement? Particularly since method calls on an interface will always be virtual anyway... :)


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      P 1 Reply Last reply
      0
      • P pr1mem0ver

        Perhaps I want something that is impossible... but thought I would ask anyway because I cannot find a way to do this... and it seems like it should be possible. I want to force the definition of a method in all subclasses without that method being virtual. (I don't want a virtual table and lookup to be necessary). Normally one would do this with an interface. The problem is that I also need to define a property with an explicit definition. Normally that is done with an abstract class (which is the only reason I need to define one) An example is illustrated below. The problem I am running into is that if I inherit the interface in the abstract class, none of the sub-classes require the "NeededFunction" to be implemented, which is counter to what I want. If I don't implement the method in the abstract class however, it gives me an error. Is there a way to do this... basically I want the derived classes to implement the "NeededFunction" rather than the abstract class (without them being virtual for performance reasons). I know I can have every derived class implement the interface instead but that seems needlessly redundant.

        interface IMyFunctionInterface
        {
        void NeededFunction(object state);
        }

        internal abstract class MyBaseImplementation : IMyFunctionInterface
        {
        internal MyRelevantProperty { get; protected set; }
        public void NeededFunction(object state)
        {
        //defined here only to satisfy interface requirements...
        //want to force this to be implemented in each subclass rather than here
        }
        }

        internal class Implementation1 : MyBaseImplementation
        {
        public void NeededFunction(object state)... // want to define this here instead of in base class
        }

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You want to "force" a method override, but do not want to implement the interface ... because it "seems redundant". How can it be "redundant" if you want to "force" the implementation of the method? You still need to add the method to the derived class and "know" if you didn't; so which part is "redundant"? In any event, you can use "new" to override a non-virtual method and complain about it using reflection.

        "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

        P 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          AFAIK, the only way to force the derived class to implement the method is to mark it as abstract. However, that would still be a virtual method.

          internal abstract class MyBaseImplementation : IMyFunctionInterface
          {
          internal Foo MyRelevantProperty { get; protected set; }
          public abstract void NeededFunction(object state);
          }

          internal class Implementation1: MyBaseImplementation
          {
          public sealed override void NeededFunction(object state) { ... }
          }

          If your base class doesn't include an interface member, then it doesn't implement the interface. All of the derived classes would need to implement the interface instead.

          pr1mem0ver wrote:

          without them being virtual for performance reasons

          Do you have any real performance data from your application to justify that requirement? Particularly since method calls on an interface will always be virtual anyway... :)


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          P Offline
          P Offline
          pr1mem0ver
          wrote on last edited by
          #4

          Richard Deeming wrote:

          If your base class doesn't include an interface member, then it doesn't implement the interface. All of the derived classes would need to implement the interface instead.

          Yeah... I have seen this suggestion elsewhere (older posts) but I get an error that the abstract class doesn't implement the method required for the interface. Is this error specific to newer versions of .NET?

          Richard DeemingR 1 Reply Last reply
          0
          • P pr1mem0ver

            Richard Deeming wrote:

            If your base class doesn't include an interface member, then it doesn't implement the interface. All of the derived classes would need to implement the interface instead.

            Yeah... I have seen this suggestion elsewhere (older posts) but I get an error that the abstract class doesn't implement the method required for the interface. Is this error specific to newer versions of .NET?

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            The "doesn't implement the method" error appears when you declare that your class implements the interface, but it doesn't provide a declaration for one or more members defined by the interface. This has always been the case since .NET 1.0, and has not changed with newer versions. There will be a semi-related change with C# 8, which will only be available to .NET Core projects. But not in the way you've described: C# 8: Default Interface Methods Implementation - CodeJourney.net[^]


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            1 Reply Last reply
            0
            • L Lost User

              You want to "force" a method override, but do not want to implement the interface ... because it "seems redundant". How can it be "redundant" if you want to "force" the implementation of the method? You still need to add the method to the derived class and "know" if you didn't; so which part is "redundant"? In any event, you can use "new" to override a non-virtual method and complain about it using reflection.

              "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

              P Offline
              P Offline
              pr1mem0ver
              wrote on last edited by
              #6

              You are not following what I mean by redundant. I mean redundant in that I have to add the Interface to every single derived class rather than just the base class. You also don't seem to get that part of the point of doing things this way is because conceptually it is more robust. This is one use of CRTP's in C++.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups