Inheritance allows a class to acquire the members of another class. In the following example, the class
Square inherits from
Rectangle
, specified by a 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 or destructors.
// 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 ...