What will be the correct modifier?

I have the following class with some methods, and I would like to use it as the base class of another class.

public class BaseClass { public string DoWork(string str) { // some codes... } // other methods... } 

I do not want this class to be created, but the derived class should still use the original implementation of the methods of its base class.

Is it possible? What should be my modifier?

+7
c #
source share
2 answers

Since you do not want this class to be created, make it an abstract class. You can still have an implementation in the class.

fragment,

 public abstract class BaseClass { public virtual string DoWork(string str) { // can have implementation here // and classes that inherits can overide this method because of virtual. } // other methods... } 
+6
source share

Make a BaseClass annotation :

 public abstract class BaseClass { // Only available to BaseClass private string _myString; public string DoWork(string str) { // Available to everyone return _myString; } protected void DoWorkInternal() { // Only available to classes who inherit base class } } 

Thus, you can define your own code in BaseClass - but it cannot be initialized directly, it must be inherited from.

+7
source share

All Articles