C# Inheritance Summary

I thought id summarise some key facilitators of inheritance in C# to provide a quick reference of when and where to use these. Im going back to the core language features here a little, just to consolidate the ideas. I confess that i don’t use a lot of these features as much as i probably should – the underlying intention of this post is to remind me to use them a bit more!

Polymorphism
Polymorphism is a term thats used frequently in OO programming and is linked to inheritance. Polymorphism basically means that different entities (ie clases) can have the same properties / methods implemented in different ways (eg by implementing overrides on a base class / interface / abstract class).

In reality it means that different classes can be called as if there they were the same (eg so you can set up a method that is the base class type – it will be able to take either class inheriting from that derived type). For instance, you can pass and object deriving from a base class to a method that accepts the base class as the parameter. You can then call methods (of the base class) within that method, even if they are overridden in a deriving class.

Note that if the deriving classes has a method that overrides the base class method, the overriding method will be used (by a method that accepts operates on the base class and calls that method). However if the deriving class has a new method that hides a bass class method, the base class method will be used.
Click here to download a VS 2005 example project illustrating these points

Abstract Classes

  • Cannot be directly instantiated
  • They can contain a mixture of normal and abstract functions.
  • Abstract functions cannot contain implementation
  • Abstract classes are different from interfaces as they can contain code, member variables etc – interfaces only expose properties
  • Class can only derive from one class (abstract or not)
  • Of course, you can have just a normal c# class acting as a base class, however this can be directly instantiated and any non-virtual methods cannot be overridden.

Interfaces

  • Cannot be directly instantiated
  • Classes can derive from multiple interfaces
  • Do not contain fields – only define properties / indexers
  • You cannot specify access keywords (everything is implicitly public)
  • You cannot declare constructors / destructors (as you cannot create an instance of an interface)
  • You cannot nest enum, classes etc in an interface
  • Interface can inherit from other interfaces (but not classes as they contain implementation).

Virtual Methods

  • Specifies that a method may be overridden – a virtual method is just the first implementation (in the base class) of the method
  • Different to abstract methods, as abstract methods contain no implementation.
  • You cannot have private virtual methods

New Keyword

  • Note that the new keyword can be used to hide a virtual method, but as it implies that the two functions are for different purposes, but have the same signature, it cannot be used with the override keyword.
  • As stated above, if the deriving classes has a method that overrides the base class method, the overriding method will be used (by a method that accepts operates on the base class and calls that method). However if the deriving class has a new method that hides a bass class method, the base class method will be used.
  • Note that the method in the base class is still hidden by the new method and cannot be accessed when the deriving class is called.
  • Note that the new keyword is also used to instantiate a new instance of a class – this usage is unrelated.

Override Keyword

  • The override method is used complimentary to the virtual method. When your deriving from a base class with a virtual method, you can override the virtual method by using the override keyword.
  • AS virtual methods cannot be private and an override to that virtual methods must have an identical signature, override methods cannot be private.
  • Override methods are implicitly virtual – they can thus be overridden in classes subsequently deriving from them.

Sealed Classes

  • Sealed classes cannot be inherited – they can only be called.
  • Cannot contain virtual methods (as these can never be overridden)

Sealed Methods

  • A sealed method cannot be overridden by a deriving class.
  • These are only used when you have an method in a deriving class overridden a virtual method in the base class, but you dont want the override method in the deriving class to ever be overridden by subsequent inheriting classes.
  • All classes can contain sealed methods (that cannot be overridden or hidden by the new keyword).

Static methods

  • Static methods on class can be called with an object instance of that class being instantiated.

Static fields

  • Static data remains fixed across different instances of a classes – in is not reinitialised when a different object is instantiated.
  • A static field is shared between all objects.
  • Also known as a shared field.

Calling base class constructors
In a deriving class, if you don’t explicitly call the bass class constructor in a derived construct, the compiler will automatically call the base class constructor passing no parameters (the base class constructor is always called when a deriving class is instantiated silently by the compiler. If you need parameters to be passed to the base class constructor, you must explicitly call the bass class constructor using the base keyword.

eg. public DerivingClassContructor(string someVariable) : base(someVariable){ }

Other Notes
Also note that a class can inherit from multiple interface, but only one class. A class can inherit from another class and also implement interfaces simultaneously.

Also note that protected access means that a inheriting classes can see that member, but anything directly calling that class cannot.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>