2

It is possible in C# do something like this

public absctract class ImportBase()
{
   public abstract void CreateDocument();
}
public class UsingOne : ImportBase
{
   public override bool CreateDocument(string name)
   {
      return null;
   }
}

I want have some Base class, which only have some methods,but in derived class i need change inputs parameters and inside of method.

3 Answers 3

4

You're not overriding the method. The point of having an abstract (or virtual) method is that given any ImportBase, I should be able to call

importBase.CreateDocument();

That's clearly not the case with UsingOne, as it needs more information. So you're really trying to tie your caller to UsingOne, not just ImportBase - at which point you've lost the benefits of polymorphism.

To override a method, the implementation has to have the same signature, basically.

Sign up to request clarification or add additional context in comments.

1 Comment

I understand. Find way to have only prescription of methods, and derived class say what the methods need and what returns
2

Probably you want to minimize the duplicate code on your derived classes. Basically it's not possible to have an override of a different signature but surely you can refactor your code where you can keep the possible duplicate code in the base class and use it on your derived classes.

public absctract class ImportBase()
{
   //Making this protected here
   protected virtual void CreateDocument() 
   {
      //Your CreateDocument code
   };
}

public class UsingOne : ImportBase
{
   private override void CreateDocument()
   {
      // Override this if you have different CreateDocument for your different
      // for different derived class.
   }
   public bool CreateDocument(string name)
   {
      // Do whatever you need to do with name parameter.
      base.CreateDocument();
      // Do whatever you need to do with name parameter.
      return true; // return false;
   }
}

You can create instance of UsingOne and invoke CreateDocument(string name)

1 Comment

The override would have to be protected rather than private.
1

nope. signature must be same on the derived class. i suggest to use builder pattern.

http://en.wikipedia.org/wiki/Builder_pattern

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.