7.9. Abstract methods

Abstract methods in C# are pretty much the same as abstract methods in Java, except for some additional rules. Here is an example.

1: abstract class A{
2:   protected abstract int ProcessInt(int i);
3: }
4:
5: class B:A{
6:   protected override int ProcessInt(int i){
7:     return i*2;
8:   }
9: }

Like Java

  • No implementation of that method is provided – overriding methods of non-abstract subclasses are expected to provide the method implementation.

  • Abstract methods cannot be private. [10]

    [10] Abstract private methods don't make sense at all since abstract methods are intended to be overridden, and private methods cannot be overridden in subclasses.

  • Classes with one or more abstract methods must be declared as an abstract class (although ...

Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.