6.5. Class inheritance

Inheritance in C# works in very much the same way as in Java, except for some differences in method overriding (see section 7.10). A class can inherit from only one superclass, and can implement multiple interfaces. Note the syntactical differences between the two languages though. [7]

[7] C# resembles C++ syntax when it comes to inheritance. There is no extends or implements keyword like in Java, just the colon.

To inherit one class from another, use the following syntax:

class <class_name>:<super_class>

The code below contains two classes, Child and Parent. Child is a subclass of Parent, and inherits the DoSomething() method.

 1: using System; 2: 3: public class Parent{ 4: protected void DoSomething(){ 5: Console.WriteLine("inherited ...

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.