-2

I have two methods defined in two classes. These methods are almost the same. Instead of these two methods I want to use only one in the abstract class but the problem is that every of these two methods is calling different method and that is only difference between this two methods.

public class Perk
{
  public void Method()
  {
     //some code goes here
     Method1();
  }
} 

public class Jerk
{
  public void Method()
  {
     //some code goes here
     Method2();
  }
} 
Mark Benningfield
  • 2,800
  • 9
  • 31
  • 31
Danilo
  • 11
  • 1
    Please define what you want and what is problem. [How to ask](https://stackoverflow.com/help/how-to-ask) – Lazar Đorđević Jan 19 '21 at 00:44
  • 1
    So many ways of handling this. Here's one, the template method pattern: `abstract class Base { public void Method() { SpecifiedByDerived(); protected abstract void SpecifiedByDerived(); } ` then `class Jerk: Base { protected override void SpecifiedByDerived() => Method2();} ` – Aluan Haddad Jan 19 '21 at 00:52
  • Please choose proper title for your questions – Mohammad Barbast Jan 19 '21 at 01:45

2 Answers2

-2

Hard to understand exactly what this question is driving at, but I'm guessing you are wanting the syntax for having one base method in an abstract class.

Instead of these two methods I want to use only one in the abstract class but the problem is that every of these two methods is calling different method and that is only difference between this two methods.

I took what you said about only wanting to use 1 method in the abstract class literally. The below example here shows one method in the abstract class... However, there is also a very confusing next sentence. I took it to mean you wanted to use polymorphism so that you could call Perk.Method(), Jerk.Method(). Each class, Perk and Jerk having a different implementation. Obviously, there is much confusion around this post :)

    public abstract class BaseAbstractClass {
        public abstract void Method();
    }

    public class FirstClass : BaseAbstractClass {

        public override void Method() {
            throw new System.NotImplementedException();
        }
    }

    public class SecondClass : BaseAbstractClass {

        public override void Method() {
            throw new System.NotImplementedException();
        }
    }

If this is not the right interpretation, and you want to just have one method in an abstract class that is implemented exactly the same for both Perk & Jerk class, don't make the method abstract, just implement it in the abstract class.

   public abstract class BaseAbstractClass {
        public abstract void Method();
   }

   public class FirstClass : BaseAbstractClass {
        // Method is on the base class
   }

    public class SecondClass : BaseAbstractClass {
        // Method is on the base class
    }
GetFuzzy
  • 2,116
  • 3
  • 26
  • 42
  • I retracted the vote. I downvoted because I thought I knew what he was asking and your answer seemed way off base – Aluan Haddad Jan 19 '21 at 01:43
  • This looks like opposite of what OP is asking - instead of reusing shared code and calling out to class specific pieces it advises to to just copy code to two methods without any visible benefits... Some clarification how that help to avoid code duplication may improve the answer. – Alexei Levenkov Jan 19 '21 at 01:44
-2

I think what you are looking for is not an abstract class, but a static one.

In an abstract class you would not implement the method and would fallback to basically the same code you have now.

abstract class Myclass
{
  public abstract void Method();
}


public class Perk : Myclass
{
  // here you would have to implement the abstract method
  public override void Method(){
      //some code goes here
      Method1();
  }
} 

public class Jerk: Myclass
{
  // here you would have to implement the abstract method
  public override void Method(){
      //some code goes here
      Method2();
  }
} 

But you could create a static class which implements the method and use a parameter to choose which path to follow.

static class Myclass
{
  public static Method(param)
  {
     //some code goes here
     if(param == param1) {
         Method1();
     }
     else if(param == param2) {
         Method2();
     }
  }
}


public class Perk : Myclass
{
  //Here you can use Myclass.Method(param1)
} 

public class Jerk: Myclass
{
  ////Here you can use Myclass.Method(param2)
} 

Baldo
  • 26
  • 4