C# Inheritance Example

Review the concept of inheritance. See a class that inherits from another.
Inheritance makes programs simpler and faster. With inheritance, we build several types upon a common abstraction. Then we later act upon all those types through the abstraction. We examine inheritance and polymorphism in C#.Class

Base class: The class another class inherits from. In the C# language, the ultimate base class is the object class.

Derived class: The class with a base class. A derived class may have multiple levels of base classes.

Example. When you specify that a class derives from another class, the class you create acquires all the features of the base class. If the derived class has a few extra features but is otherwise the same as the base class, this is a good approach.

Also: By using a base class, you can treat derived classes as the same base type and call virtual functions to access their features.

Program: This code shows 2 derived classes from 1 base class. We create a List of the base class type, and then add derived classes to it.

List

Finally: We call Act() through the base class references in the List. This invokes the overridden functions through the virtual base function.

Thus: The derived types retain all their features but share a type. This is an example of object-oriented programming.

C# program that uses base class, virtual method using System; using System.Collections.Generic; class Net { public virtual void Act() { } } class Perl : Net { public override void Act() { Console.WriteLine("Perl.Act"); } } class Python : Net { public override void Act() { Console.WriteLine("Python.Act"); } } class Program { static void Main() { // Use base class and derived types in a List. List<Net> nets = new List<Net>(); nets.Add(new Perl()); nets.Add(new Python()); // Call virtual method on each instance. foreach (Net net in nets) { net.Act(); } } } Output Perl.Act Python.Act
Casting. It is possible to use cast operators to test or convert a base type to a more derived type. However, in many cases, if your program uses a base class but has to cast to more derived types, it may have a design flaw.

Note: It is often better to introduce a new virtual method on the base class and implement it in the derived type.

AsIsVirtual
Multiple base classes. You cannot specify multiple base classes on a type declaration. This restriction was imposed on the C# language to reduce the complexity of inheritance and eliminate some problems. You can, however, implement multiple interfaces.

Also: You can combine the concepts—both implement interfaces and inherit from a single base class.

InterfaceIEnumerable
Error 1: Class Python cannot have multiple base classes: Net and Perl.
Summary. Inheritance enables you to create complex models that can reduce your program size and improve its modularity and performance. It is important, though, not to overuse inheritance.

Often: Simpler solutions, where a common abstraction is not specified, are superior.

© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
HomeSearch
Home
Dot Net Perls