6.8. Abstract classes

The idea of abstract classes in C# is pretty much identical to that in Java – a class which cannot be instantiated, and which may contain abstract methods.

There follows an example of an abstract class, MyAbstractClass, which contains one abstract method, DoSomething(). MyClass inherits from MyAbstractClass and provides an implementation of the method. [14]

[14] On line 8, the override modifier is used to declare that DoSomething() is overriding the abstract method of the same name in the superclass.

 1: using System;
 2:
 3: public abstract class MyAbstractClass{
 4:   public abstract int DoSomething();
 5: }
 6:
 7: public class MyClass:MyAbstractClass{
 8:   public override int DoSomething(){ 9: return 0; 10: } 11: 12: public static ...

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.