O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required