Polymorphism
Everybody's favourite.
| code: | using System;
public class Dog
{
// Notice how both methods have the same name
public void MakeNoise()
{
// No parameters? Bark!
Console.WriteLine("Bark!");
}
public void MakeNoise(string noise)
{
// Otherwise make the noise specified
Console.WriteLine("{0}!", noise);
}
static void Main()
{
// Create a new instance of the Dog class
Dog lassieTheRetriever = new Dog();
// Sometimes Lassie may bark, sometimes she may
// yip, it depends on the situation.
lassieTheRetriever.MakeNoise();
lassieTheRetriever.MakeNoise("Yip");
}
} |
Output:
Inheritance
Unfortunately C# does not support multiple inheritance, just regular inheritance. Still very simple to accomplish via the use of a colon.
| code: | using System;
public class Dog
{
// Every healthy dog can walk, right?
public void Walk(int distance)
{
Console.WriteLine("Walked {0}m", distance);
}
public static void Main()
{
// Create a new instance of GoldenRetriever named
// lassie, then walk 10m
GoldenRetriever lassie = new GoldenRetriever();
lassie.Walk(10);
}
}
// Empty class we will take advantage later
public class GoldenRetriever : Dog
{
} |
Output:
Method Overriding
You may run into instances where you are required to override a method entirely instead of using polymorphism. Virtual methods come in very handy for this.
| code: | using System;
public class Dog
{
// Many dogs have their own tricks
public virtual void PerformTrick()
{
// This method will be overwritten later :-)
}
public static void Main()
{
// Create a new instance of GoldenRetriever named lassie
GoldenRetriever lassie = new GoldenRetriever();
// Lassie needs to do a trick now
lassie.PerformTrick();
}
}
public class GoldenRetriever : Dog
{
public override void PerformTrick()
{
Console.WriteLine("Fetch!");
}
} |
Output:
Properties
Properties allow us to change certain features of an object while still controlling access, thereby allowing us to place restrictions on the way the information is accessed and changed.
| code: | using System;
public class Dog
{
// Every dog has some properties, they just vary
// from dog to dog.
// We don't want anybody but the setters and getters
// touching our properties. Otherwise they may touch them
// in inappropriate ways. By using this method we restrict
// ways the values can be modified.
private int _numberOfLegs = 4;
private string _furColour = "Blonde";
// Notice how these "methods" contain no parameters nor
// parenthesis and their names match those of the property
// variables, only they are in Pascal Case instead.
public int NumberOfLegs
{
get {
// Also notice the prefix on property variables
return _numberOfLegs;
} set {
// value is a special object that holds
// the new value passed to this block.
_numberOfLegs = value;
}
}
public string FurColour
{
get { return _furColour; }
set { _furColour = value; }
}
public static void Main()
{
// Create a new instance of GoldenRetriever named lassie
GoldenRetriever lassie = new GoldenRetriever();
Console.WriteLine("Lassie\'s coat is {0} in colour and she has {1} legs", lassie.FurColour, lassie.NumberOfLegs);
// Create another GoldenRetriever object named rebelBailey
GoldenRetriever rebelBailey = new GoldenRetriever();
// Sadly he only has 3 legs, and a purple coat
rebelBailey.NumberOfLegs = 3;
rebelBailey.FurColour = "Purple";
// Now display Rebel Bailey's modified properties
Console.WriteLine("Rebel Bailey\'s coat is {0} in colour and he has {1} legs.", rebelBailey.FurColour, rebelBailey.NumberOfLegs);
}
}
// A breed or "subclass" of Dog
public class GoldenRetriever : Dog
{
}
|
Output:
| code: |
Lassie's coat is Blonde in colour and she has 4 legs
Rebel Bailey's coat is Purple in colour and he has 3 legs. |
|