Tuesday, July 28, 2009

Demonstrating Inheritance in C# - The Animal Class

The Animal class:


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

namespace InheritanceAnimals
{
public class Animal
{
//fields
private bool legs;
private bool wings;

//properties
public bool Legs
{
get { return legs; }
set { legs = value; }
}
public bool Wings
{
get { return wings; }
set { wings = value; }
}

//constructors

public Animal(bool _legs, bool _wings)
{
this.legs = _legs;
this.wings = _wings;
}

//methods
public virtual string eat()
{
return " unknown";
}

public virtual string hair()
{
return " unknown";
}

public virtual string sound()
{
return " unknown";
}

public virtual string movement()
{
return " unknown";
}

}
}


The Bat class:


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

namespace InheritanceAnimals
{
public class Bat : Animal
{
//fields

//properties

//constructors
public Bat():base(true,true)
{
}


public override string eat()
{
return "fruit and insects";
}

public override string hair()
{
return "feathers";
}


public override string movement()
{
return "unknown";
}

public override string sound()
{
return "unknown";
}

public string take_off()
{
return "launches from the tree";
}

public string land()
{
return "hangs on a rafter";
}


}
}


The Hawk class:


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

namespace InheritanceAnimals
{
public class Hawk : Animal
{
//fields

//properties

//constructors
public Hawk():base(true,true)
{

}

public override string eat()
{
return "small animals";
}

public override string hair()
{
return "feathers";
}

public override string sound()
{
return "Screeches";
}

public override string movement()
{
return "unknown";
}

public string take_off()
{
return "glides";
}

public string land()
{
return "perches on a tree top";
}


}
}


The Monkey class:


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

namespace InheritanceAnimals
{
public class Monkey : Animal
{
//fields

//properties

//constructors
public Monkey() : base(true, false)
{

}

public override string eat()
{
return "fruit";
}

public override string hair()
{
return "fur";
}

public override string sound()
{
return "Chatters";
}

public override string movement()
{
return "jumps";
}
}
}


The Snake class:


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

namespace InheritanceAnimals
{
public class Snake : Animal
{
//fields

//properties

//constructors
public Snake(): base(false,false)
{
}

public override string eat()
{
return "rats";
}

public override string hair()
{
return "none";
}

public override string sound()
{
return "Hisses";
}

public override string movement()
{
return "slithers";
}

}
}


And finally, the GUI form sourcecode:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace InheritanceAnimals
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnShow_Click(object sender, EventArgs e)
{
Snake snake = new Snake();
Monkey monkey = new Monkey();
Bat bat = new Bat();
Hawk hawk = new Hawk();

string myresults = "";

myresults = "Snake (" + snake.Legs + " " + snake.Wings + ") eats " + snake.eat() + ", hair = " + snake.hair() + ", sound = " + snake.sound() + ", moves = " + snake.movement() +"\n\n";
myresults += "Monkey (" + monkey.Legs + " " + monkey.Wings + ") eats " + monkey.eat() + ", hair = " + monkey.hair() + ", sound = " + monkey.sound() + ", moves = " + monkey.movement() + "\n\n";
myresults += "Bat (" + bat.Legs + " " + bat.Wings + ") eats " + bat.eat() + ", hair = " + bat.hair() + ", sound = " + bat.sound() + ", moves = " + bat.movement() + ", takes off = " + bat.take_off() + ", lands = " + bat.land() + "\n\n";
myresults += "Hawk (" + hawk.Legs + " " + hawk.Wings + ") eats " + hawk.eat() + ", hair = " + hawk.hair() + ", sound = " + hawk.sound() + ", moves = " + hawk.movement() + ", takes off = " + hawk.take_off() + ", lands = " + hawk.land() + "\n\n";
lblResult.Text = myresults;
}

}
}

2 comments:

  1. You posted this in class? So fast...! After class I implemented some code to use inheritance more. I created a common method in Animal class and I overrided that metod in Bat and Hawk class.I can't explain well, it means the below...

    //Animal class
    public virtual string display()
    {
    return "legs:" + legs + "\n" +
    "wings:" + wings + "\n" +
    "eat:" + eat() + "\n" +
    "hair:" + hair() + "\n" +
    "sound:" + sound() + "\n" +
    "move:" + move();
    }

    //Hawks & Bat class
    public override string display()
    {
    return base.display() +
    "takeoff:" + takeoff() + "\n" +
    "land:" + land() + "\n";
    }

    //Form
    label1.Text =
    "Monkey" + "\n" + m1.display() + "\n\n" +
    "Snake" + "\n" + s1.display() + "\n\n" +
    "Bat" + "\n" + b1.display() + "\n\n" +
    "Hawk" + "\n" + h1.display() + "\n\n";
    }

    ReplyDelete
  2. That's great! I knew that there was a better way to override those methods! I am not very familiar with the use of base.method() but now, I know thanks to you :)
    Great comment, maybe you should post and share your code on the blog so that we can all see yours.

    BTW, I was giving some thoughts on the class Monkey, and I am pretty sure that the Class Mahmoud (extends/inherits from) that superclass...

    ReplyDelete