Skip to Main Content
C# 2008 Programmer's Reference
book

C# 2008 Programmer's Reference

by Wei-Meng Lee
November 2008
Intermediate to advanced content levelIntermediate to advanced
838 pages
16h 26m
English
Wrox
Content preview from C# 2008 Programmer's Reference

Chapter 6. Inheritance

Inheritance is one of the fundamental concepts in object-oriented programming. Inheritance facilitates code reuse and allows you to extend the functionality of code that you have already written. This chapter looks at:

  • How inheritance works

  • Implementing inheritance in C#

  • Defining abstract methods and classes

  • Sealing classes and methods

  • Defining overloaded methods

  • The different types of access modifiers you can use in inheritance

  • Using inheritance in interfaces

Understanding Inheritance in C#

The following Employee class contains information about employees in a company:

public class Employee
{
    public string Name { get; set; }
    public DateTime DateofBirth { get; set; }
    public ushort Age()
    {
        return (ushort)(DateTime.Now.Year - this.DateofBirth.Year);
    }
}

Manager is a class containing information about managers:

public class Manager
{
    public string Name { get; set; }
    public DateTime DateofBirth { get; set; }
    public ushort Age()
    {
        return (ushort)(DateTime.Now.Year - this.DateofBirth.Year);
    }
    public Employee[] subordinates { get; set; }
}

The key difference between the Manager class and the Employee class is that Manager has an additional property, subordinates, that contains an array of employees under the supervision of a manager. In fact, a manager is actually an employee, except that he has some additional roles. In this example, the Manager class could inherit from the Employee class and then add the additional subordinates property that it requires, like this:

public class ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C# 5.0 Programmer's Reference

C# 5.0 Programmer's Reference

Rod Stephens
Learning C# 2005, 2nd Edition

Learning C# 2005, 2nd Edition

Jesse Liberty, Brian MacDonald
A Programmer's Guide to C# 5.0, 4th Edition

A Programmer's Guide to C# 5.0, 4th Edition

Eric Gunnerson, Nick Wienholt
Beginning C# 6.0 Programming with Visual Studio 2015

Beginning C# 6.0 Programming with Visual Studio 2015

Benjamin Perkins, Jacob Vibe Hammer, Jon D. Reid

Publisher Resources

ISBN: 9780470285817Purchase book