Difference between revisions of "Polymorphism - 2017"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "Polymorphism is where a sub class inherits all the methods and properties from the parent class. However, The sub class may override the methods or properties of the parent cl...")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
Polymorphism is where a sub class inherits all the methods and properties from the parent class. However, The sub class may override the methods or properties of the parent class, for example both The blue whale and A human are examples of a Mammal, however a classical mammal "Walk" a blue whale over rides this by "swim".
 
Polymorphism is where a sub class inherits all the methods and properties from the parent class. However, The sub class may override the methods or properties of the parent class, for example both The blue whale and A human are examples of a Mammal, however a classical mammal "Walk" a blue whale over rides this by "swim".
 +
 +
In the skeleton program:
 +
<syntaxhighlight lang="csharp">
 +
public virtual void Inspect()
 +
    {
 +
      Console.Write("  ID " + ID + " ");
 +
      Console.Write("Age " + Age + " ");
 +
      Console.Write("LS " + NaturalLifespan + " ");
 +
      Console.Write("Pr dth " + Math.Round(ProbabilityOfDeathOtherCauses, 2) + " ");
 +
    }
 +
</syntaxhighlight>
 +
Override:
 +
<syntaxhighlight lang="csharp">
 +
public override void Inspect()
 +
    {
 +
      base.Inspect();
 +
      Console.Write("Rep rate " + Math.Round(ReproductionRate, 1) + " ");
 +
      Console.WriteLine("Gender " + Gender + " ");
 +
    }
 +
</syntaxhighlight>

Latest revision as of 14:28, 13 February 2017

Polymorphism is where a sub class inherits all the methods and properties from the parent class. However, The sub class may override the methods or properties of the parent class, for example both The blue whale and A human are examples of a Mammal, however a classical mammal "Walk" a blue whale over rides this by "swim".

In the skeleton program:

 public virtual void Inspect()
    {
      Console.Write("  ID " + ID + " ");
      Console.Write("Age " + Age + " ");
      Console.Write("LS " + NaturalLifespan + " ");
      Console.Write("Pr dth " + Math.Round(ProbabilityOfDeathOtherCauses, 2) + " ");
    }

Override:

public override void Inspect()
    {
      base.Inspect();
      Console.Write("Rep rate " + Math.Round(ReproductionRate, 1) + " ");
      Console.WriteLine("Gender " + Gender + " ");
    }