CHAPTER 11

image

Inheritance

Inheritance allows a class to acquire the members of another class. In the example below, the class Square inherits from Rectangle, specified by the colon. Rectangle then becomes the base class of Square, which in turn becomes a derived class of Rectangle. In addition to its own members, Square gains all accessible members in Rectangle, except for any constructors and destructor.

// Base class (parent class)class Rectangle{  public int x = 10, y = 10;  public int GetArea() { return x * y; }}  // Derived class (child class)class Square : Rectangle {}

Object class

A class in C# may only inherit from one base class. If no ...

Get C# Quick Syntax Reference 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.