#333 – Class Inheritance Leads to a Hierarchy of Classes

Defining a new class, you can specify that it inherits data and behavior from any other existing class, or you can allow the new class to implicitly inherit from System.Object.  Because the new class can inherit from a class that itself inherits from another class, you end up defining a hierarchy of parent / child (base class / derived class) relationships.

Consider the picture shown below.  The user has defined a Dog class, which implicitly inherits from the predefined System.Object class.  They also define the Hound and Terrier classes, both of which inherit from Dog, as well as the FoxTerrier class, which inherits from Terrier.

333-005

A class inherits data and methods not only from its immediate parent, but also from other classes higher up in the inheritance chain.  For example, the FoxTerrier class inherits data and methods from Terrier, but also from the Dog class, as well as the System.Object class.

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

4 Responses to #333 – Class Inheritance Leads to a Hierarchy of Classes

  1. Pingback: #567 – Wider vs. Narrower Types « 2,000 Things You Should Know About C#

  2. wekelly says:

    Sean, Love your posts.

    In this case, you have an error. String is a sealed class. So your class DogName : string is a bit unrealistic. You could do something like class DogComparer : Comparer { } or class Kennel : List if you want to show deriving from a BCL class other than Object. Or struct DogName derives from System.ValueType.

    • Sean says:

      Ah yes, you’re right–bad example to show inheriting from a sealed class. That’s what I get for not backing this post with actual code, to double-check myself. :O) Thanks!

  3. Pingback: #685 – Inheritance Can Break Encapsulation « 2,000 Things You Should Know About C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: