P
P
pmatveev2014-03-14 19:43:31
OOP
pmatveev, 2014-03-14 19:43:31

Inheritance in C#: changing the return type - how to fix the error?

abstract class ALauncher
   {
       protected abstract Object parseArgs(string[] args);
   }

   class Launcher : ALauncher
   {
       protected override ABitFileManager parseArgs(string[] args) {...}
   }

Why does the inheritance error occur? After all, ABitFileManager is an inheritor of Object (like any other class)?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2014-03-14
@pmatveev

C# does not support return type covariance.

A
Anton, 2014-03-16
@Nirvano

Generalizations can probably help you.

interface ILauncher
  {
    object ParseArgs(string[] args);
  }

  abstract class ALanucher<T> : ILauncher
  {
    protected abstract T ParseArgs(string[] args);

    object ILanucher.ParseArgs(string[] args)
    {
      return ParseArgs(args);
    }
  }

  class Launcher : ALanucher<int>
  {
    protected override int ParseArgs(string[] args)
    {
      throw new NotImplementedException();
    }
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question