Some trouble with private abstract methods

Let's say I make a major class Car - and I want this class to be abstract. Abstract because this is my major class, nobody should make an object of this class. This class should be only there as "backbone".

I want that classes can be created only from subclasses of Car (lets say Mercedes, Ferrari...). Because every car should have methods like StartEngine I put it into the major class. Let's say I have this:

abstract class Car
{
  public string Name { get; set; }

  public abstract void StartEngine();
  private abstract bool CheckGasoline();
  //and so on...
}

class Mercedes : Car
{
  private override bool CheckGasoline()
  {
  //somehow check gasoline and return whatever...
  }

  public override void StartEngine()
  {
    if (CheckGasoline())
      //start engine...
  }
}

Well this is not gonna work. Because of private abstract:

virtual or abstract members cannot be private

So ill make every private method to protected:

abstract class Car
{
  public string Name { get; set; }

  public abstract void StartEngine();
  protected abstract bool CheckGasoline();
  //and so on...
}

class Mercedes : Car
{
  protected override bool CheckGasoline()
  {
  //somehow check gasoline and return whatever...
  }

  public override void StartEngine()
  {
    if (CheckGasoline())
      //start engine...
  }
}

Is this alright? I mean it's working, but is that how it should be? Using protected when I just need a method in the same class (like here: CheckGasoline() is only needed for StartEngine() ). Somehow private would look better.

Any suggestions? Thank you.

Answers


Yes that is fine. A sub-type cannot see private methods, therefore cannot override them: they must be protected (or public etc). There is no such thing as "private to method X" in c#, so it'll have to suffice as-is.


Private methods are inaccessible to any class other than the classes that they are in - this includes derived classes.

Protected methods, on the other hand, are accessible both to the classes that they are in AND their derived classes.

Your usage of protected is correct.

You may find this article to be of help: http://msdn.microsoft.com/en-us/library/ba0a1yw2(v=vs.80).aspx.


You can build in override hooks. Microsoft does this with FrameworkElement.ArrangeCore(...) (which utilizes ArrangeOverride) and FrameworkElement.MeasureCore(...) (which utilizes MeasureOverride).

For example:

abstract class Car
{
  public string Name { get; set; }

  public void StartEngine()
  {
    if (CheckGasoline())
        StartEngineOverride();
  }

  protected abstract void StartEngineOverride();
  protected abstract bool CheckGasoline();

  // ...
}

class Mercedes : Car
{
  protected override bool CheckGasoline()
  {
     //somehow check gasoline and return whatever...
  }

  protected override void StartEngineOverride()
  {
     // CheckGasoline was already checked, so just do the override portion.
  }
}

I reccomend the following:

Try using protected for your member variable declarations in the abstract class. Then use public for your abstract methods, as well.

Next, within your derived class, you should use public and override for your methods in that derived class. However, this is another option that you have, and that is that you could use virtual as well. Therefore, instead of using a single abstract class, nest all of your potential Car type class within one public CarClass class, or whatever you want to call it.

Use the virtual method for a shared method in the first declared class which is an Abstract Car class. Then use public override for the remainder of the declared derived classes that are nested also within the CarClass class which can override the virtual method that you declared in your asbstract Car class. This is another option that you have, although it may limit you to only whatever you declare in the CarClass than a distinct and separate abstract class.


Need Your Help

Android Studio does not recognize my Xiaomi Redmi Note 3

android android-studio drivers

When I try to "run" app via Android Studios, my Xiaomi Redmi Note 3 doesnt pop up in the list. However, it does show up in my PC file explorer.

You must feed a value for placeholder tensor 'Placeholder' with dtype float

python tensorflow

I'm a newer to tensorflow, I really don't know how to solve the problem.