What is the difference between Inheritance and Polymorphism

First off, what are inheritance and polymorphism?

Taken from one of my favorite books- The Object-Oriented Thought Process by Matt Weisfield, the two areas are described as:

Inheritance – A Class can inherit from another class and take advantage of the attributes and methods defined by the superclass.

Polymorphism – Similar objects can respond to the same message in different ways. For example you might have a system with many shapes. However, a circle, square and a star are each drawn differently. Using polymorphism, you can send each of these shapes the same message ( draw) and each shape is responsible for drawing itself.

So, what is the difference?

Inheritance inherits it behavior from its parent class (although it can always rebel and do different things)  – it is a ‘Is a relationship’

Polpymorphism dictates that the behavior isn’t confirmed until run time of the application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
 class Program
 {
  static void Main(string[] args)
  {

   Bird MotherHen = new Hen("Mother Hen");
   MotherHen.Fly();
   MotherHen.Peck();

   Bird chick1 = new Chick("Chicken Little");
   chick1.Fly();
   chick1.Peck();

   Bird Kiwi = new BadEgg("Bruce");
   Kiwi.Fly();

   Bird Puppet = new Orville();
   Puppet.Fly();
   Puppet.Peck();

   Console.ReadLine();
  }
 }

 public abstract class Bird
 {
  public abstract void Peck();
  public abstract void Fly();
 }

 class Orville : Bird
 {
  string name = "Orville";
  public override void Peck()
  {
   Console.WriteLine(string.Format("{0} - Doesn't eat as its a puppet", name));
  }

  public override void Fly()
  {
   Console.WriteLine(string.Format("{0} - as a its a puppet", name));
  }
 }

 class Hen : Bird
 {

  public string name { get; set; }
  public Hen()
  {
   name = "Bob";
  }

  public Hen(string Name)
  {
   name = Name;
  }

  public override void Peck()
  {
   Console.WriteLine(string.Format("{0} - Peck Peck", name));
  }

  public override void Fly()
  {
   Console.WriteLine(string.Format("{0} - fly not far", name));
  }
 }

 class Chick : Hen
 {

  public Chick(string chickName)
  {
   name = chickName;
  }

  public override void Fly()
  {
   Console.WriteLine(string.Format("{0} - Flew over the hen house", name));
   base.Fly();
  }

  public override void Peck()
  {
   Console.WriteLine(string.Format("{0} - Peck Burp Churp", name));
  }

 }

 class BadEgg : Hen
 {

  public BadEgg(string chickName)
  {
   this.name = chickName;
  }

  public override void Fly()
  {
   Console.WriteLine(string.Format("{0} - as a Kiwi cannot fly",name));
  }

 }
}

As you can see the different types of bird implement behavior differently, but they all come from the same base class. It is at run time that the program decides which one to run. – Polymorphism

The inheritance example in the above is when the fly method is called within Chick – it does a bit of its won thing, but then passes it back up the chain to “Mum” to handle as well.

 

 

Advertisements