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. Can we overload an abstract method of an abstract class?

Can we overload an abstract method of an abstract class?

Scheduled Pinned Locked Moved C#
helpquestion
4 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.
  • N Offline
    N Offline
    NJdotnetdev
    wrote on last edited by
    #1

    I have 3 classes overriding a function of base abstract class A. The child classes are 1, 2 & 3. Child classes override function "public abstract void hello(var1, var2)".

    Following is what I want to implement:
    abstract class A
    {
    public abstract void hello(var1, var2); // This will be only for class 1 & class 2 (Existing function)
    public abstract void hello(var1, var2, var3); // Want to make this available for class 3 only. (Want to add this new function)
    }

    ** Currently the above code throws error and wants me to implement the new inherited abstract function in class 1 & 2.

    Richard DeemingR M 2 Replies Last reply
    0
    • N NJdotnetdev

      I have 3 classes overriding a function of base abstract class A. The child classes are 1, 2 & 3. Child classes override function "public abstract void hello(var1, var2)".

      Following is what I want to implement:
      abstract class A
      {
      public abstract void hello(var1, var2); // This will be only for class 1 & class 2 (Existing function)
      public abstract void hello(var1, var2, var3); // Want to make this available for class 3 only. (Want to add this new function)
      }

      ** Currently the above code throws error and wants me to implement the new inherited abstract function in class 1 & 2.

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

      No, an abstract method in class A must be implemented by all (non-abstract) derived classes. If the new method is only available in class 3, then add it to class 3. You can't add it to class A, because it's not valid for every instance of a class derived from class A.


      "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

      N 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        No, an abstract method in class A must be implemented by all (non-abstract) derived classes. If the new method is only available in class 3, then add it to class 3. You can't add it to class A, because it's not valid for every instance of a class derived from class A.


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

        N Offline
        N Offline
        NJdotnetdev
        wrote on last edited by
        #3

        Thanks Richard.

        1 Reply Last reply
        0
        • N NJdotnetdev

          I have 3 classes overriding a function of base abstract class A. The child classes are 1, 2 & 3. Child classes override function "public abstract void hello(var1, var2)".

          Following is what I want to implement:
          abstract class A
          {
          public abstract void hello(var1, var2); // This will be only for class 1 & class 2 (Existing function)
          public abstract void hello(var1, var2, var3); // Want to make this available for class 3 only. (Want to add this new function)
          }

          ** Currently the above code throws error and wants me to implement the new inherited abstract function in class 1 & 2.

          M Offline
          M Offline
          maddymaddy14
          wrote on last edited by
          #4

          You implementation force Class 1 and 2 to implement "hello(var1, var2,var3)" just because you want to have a extra method for Class 3. This is not a good design to follow. You should follow Open to extend and close to modification methodology. you might create an extension of class A and implement it for Class 3. Please refer the approach below:

          abstract class A
          {
          public abstract void hello(var1, var2); // This will be only for class 1 & class 2 (Existing function)
          }
          abstract class A1 : A
          {
          public abstract void hello(var1, var2, var3); // Want to make this available for class 3 only. (Want to add this new function)
          }

          Implement Class A for Class 1 and Class 2 Implement Class A1 for Class 3

          Maddy

          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