O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Polymorphism

The final pillar of object-oriented programming is known as polymorphism, from the Greek word that stands for “many shapes.” In fact, when talking about inheritance, we have already seen most of the polymorphic principles illustrated at the level of member declarations. More specifically, the virtual and override keywords are at the heart of the principle of polymorphism. What we haven’t talked about yet is the different uses it allows. So far, we’ve used virtual and override to be able to specialize a class:

class NonNegativeCounter : Counter{    public override int Decrement()    {        if (Count == 0)            throw new InvalidOperationException("Can't count below zero.");        return base.Decrement(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required