#445 – Differences Between an Interface and an Abstract Class

An interface provides a list of members, without an implementation, that a class can choose to implement.  This is similar to an abstract class, which may include abstract methods that have no implementation in the abstract class, but might also include an implementation for some of its members.

One difference is that an abstract class might include some members that are fully implemented in the abstract class.  Interfaces can’t include the implementations of any of their members.  An interface just describes what a class does, while an abstract class may define how something is done.

Another difference is that a class can inherit from multiple interfaces, but can inherit from at most one base class.  Abstract classes allow you to treat an object polymorphically, based on any of the classes in its inheritance chain.  Interfaces let you treat a class polymorphically, based on any of the interfaces it implements.

#360 – Property Modifiers Required for Polymorphic Behavior

There are three combinations of modifiers that make sense, in determining whether properties in a class are virtual or non-virtual.

Typical combinations of modifiers for base class / derived class (assuming that the property’s name and type are the same in both the base and derived class):

  • (no modifier) / new – Both properties are non-virtual, derived class property hides the base class property
  • virtual / override – Both properties are virtual and support polymorphic behavior
  • virtual / new – Base class property is virtual, derived class property is non-virtual, derived class property hides the base class property

There are two other combinations that are allowed, but result in a compiler warning indicating that you should use new in the derived class to be explicit:

  • (no modifier) / (no modifier) – is equivalent to: (no modifier) / new
  • virtual / (no modifier) – is equivalent to: virtual / new

#359 – The Difference Between Virtual and Non-Virtual Properties

In C#, virtual properties support polymorphism, by using a combination of the virtual and override keywords.  With the virtual keyword on the property in the base class and the override keyword on the property in the derived class, both properties are said to be virtual.

Properties that don’t have either the virtual or override keywords, or that have the new keyword, are said to be non-virtual.

When you read or write a virtual property through an object reference, the run-time type of the object is used to determine which implementation of the property to use.

When you read or write a non-virtual property through an object reference, the compile-time type of the object is used to determine which implementation of the property to use.

#358 – Virtual Properties Support Polymorphism

In C#, polymorphism is implemented using virtual members–which can be methods, properties, indexers or events.

A virtual property has an implementation in the base class that can be overridden in a derived class.  When the property is read or written, the get or set accessor that is used is determined at run-time based on the type of the underlying object.

A virtual property is defined in the base class using the virtual keyword.

        protected string temperament;
        public virtual string Temperament
        {
            get
            {
                return string.Format("{0} is {1}", Name, temperament);
            }
        }

A virtual property is overridden in a derived class using the override keyword.

        public override string Temperament
        {
            get
            {
                return string.Format("Terrier {0} is {1}", Name, temperament);
            }
        }

Using the property:

            Dog kirby = new Dog("Kirby", 15);
            Console.WriteLine(kirby.Temperament);  // Kirby is Average

            Dog jack = new Terrier("Jack", 15);
            Console.WriteLine(jack.Temperament);   // Terrier Jack is Surly

#356 – Hidden Base Class Property Is Used Based on Declared Type of Object

When you use the new modifier to hide a base class property, its property accessors will still be called by objects whose type is the base class.  Objects whose type is the derived class will use the new property in the derived class.

            Dog kirby = new Dog("Kirby", 15);
            kirby.Temperament = "EAGER";
            Console.WriteLine(kirby.Temperament);  // Kirby is eager

            Terrier jack = new Terrier("Jack", 15);
            jack.Temperament = "CraZY";
            Console.WriteLine(jack.Temperament);  // Terrier Jack is CRAZY

We could also use a variable of type Dog (the base class) to refer to an instance of a Terrier (the derived class).  If we then reference the Temperament property using this base class variable, the Temperament property in the base class is used, even though we’re working with an instance of the derived class.

            Dog kirby = new Dog("Kirby", 15);
            kirby.Temperament = "EAGER";      // Uses Dog.Temperament

            Dog jack = new Terrier("Jack", 15);
            jack.Temperament = "CraZY";       // Also uses Dog.Temperament

#350 – Method Modifiers Required for Polymorphic Behavior

There are three combinations of method modifiers that make sense, in determining whether methods in a class are virtual or non-virtual.

Typical combinations of modifiers for base class / derived class (assuming that method signature is the same in both the base and derived class):

  • (no modifier) / new – Both methods are non-virtual, derived class method hides the base class method
  • virtual / override – Both methods are virtual, support polymorphic behavior
  • virtual / new – Base class method virtual, derived class method non-virtual, derived class method hides base class method

There are two other combinations that are allowed, but result in a compiler warning indicating that you should use new in the derived class to be explicit:

  • (no modifier) / (no modifier) – effectively (no modifier) / new
  • virtual / (no modifier) – effectively virtual / new

#349 – The Difference Between Virtual and Non-Virtual Methods

In C#, virtual methods support polymorphism, by using a combination of the virtual and override keywords.  With the virtual keyword on the base class method and the override keyword on the method in the derived class, both methods are said to be virtual.

Methods that don’t have either the virtual or override keywords, or that have the new keyword, are said to be non-virtual.

When a virtual method is invoked on an object, the run-time type of the object is used to determine which implementation of the method to use.

When a non-virtual method is invoked on an object, the compile-time type of the object is used to determine which implementation of the method to use.

#347 – Another Example of Polymorphism

The power of polymorphism is that you can invoke methods in derived classes using a reference to the base class.

Assume that you have a Dog base class and a series of classes that derive from Dog.  You might define a generic Bark method in the base class and then a Bark method specific to each breed in each of the derived classes.

You can write a method that takes any Dog as a parameter and invokes that dog’s Bark method.

    static void BarkAndTell(Dog d)
        {
            d.Bark();
            Console.WriteLine("{0} just barked", d.Name);
        }

You can pass instances of the derived classes into this method.

            Terrier jack = new Terrier("Jack", 15);
            Shepherd kirby = new Shepherd("Kirby", 12);

            BarkAndTell(jack);
            BarkAndTell(kirby);

Polymorphism ensures that the  Bark method in the appropriate subclass is called, even though we’re referring to the instance using a variable whose type is the parent class (Dog).

#346 – Polymorphism

Recall that polymorphism is one of the three core principles of object-oriented programming.

Polymorphism is the idea that the same code can act differently, depending on the underlying type of the object being acted upon.  The type of the object is determined at run-time, rather than at compile-time.

In C#, you can use a variable declared as a base type to refer to instances of one or more derived types.  Polymorphism allows you to call a method that exists in the base type but whose implementation exists in the derived types.  The appropriate method in the derived type will be called, based on the type of the object.

            Dog d;

            d = new Terrier("Jack", 15);
            d.Bark();      // Terrier.Bark is called

            d = new Shepherd("Kirby", 12);
            d.Bark();      // Shepherd.Bark is called

#229 – The Core Principles of Object-Oriented Programming

As an object-oriented language, C# supports the three core principles of object-oriented programming:

  • Encapsulation – Hide implementation details in a class from users of the class, exposing only a public interface
  • Inheritance – Derive a subclass from a parent class, inheriting data and behavior from the parent, in an “is-a” relationship.  Inheritance defines a hierarchy of classes.  All classes ultimately inherit from System.Object.
  • Polymorphism – Any subtype may be used where a parent type (or type higher up in the class hierarchy) is expected.  Conversely, a variable of a particular class will be treated as the appropriate subclass.