Go to Add > New Project and select Console App (.NET Framework)
Name it Inheritance
Right click on the project and add an Animal.cs class, a Bear.cs class, a Cat.cs class, and a PolarBear.cs class
Discussion
Inheritance is one of the key concepts to nail down in object oriented programming. Inheritance makes it easier to maintain bigger applications by defining a class based off of another class. This also allows us to reuse code functionality and speed up implementation time.
When creating a new class we can designate it to inherit the members of an existing class instead of having to completely write new data members and functions. The existing class is called the base class, and the new class is referred to as the derived class.
The idea of inheritance implements the IS-A(n) relationship. For example, mammal IS An animal, dog IS-A mammal hence dog IS-An animal as well, and so on. Lets look an example.
First lets make our base class, the class the rest of our animal classes will inherit from
We make a field that will hold the data for our _latinName
Make a property that gets the _latinName and returns based on the condition
Make other properties that make up your basic animal
Make methods that make up your basic animal
publicclassAnimal{ //Fieldsstring_latinName; //Properties - get() & set() propertiespublicstringLatinName{get{return_latinName;}set{if(String.IsNullOrWhiteSpace(value)){thrownewArgumentException("Name can't be empty");}else{_latinName=value.Trim();}}}publicstringHabitat{get;set;}publicintNumberOfLegs{get;set;}publicintWeight{get;set;}publicintAverageNumOffspring{get;set;}publicboolHasFur{get;set;}publicboolHasWings{get;set;}publicboolCanFly{get;set;} //Methods - execute some function //Need to discuss override-ability that virtual keyword gives.publicvirtualvoidStateType(){Console.WriteLine("I am an animal");}publicvirtualvoidGetMad(){Console.WriteLine("ROAR!!");}}
Next, we make our Bear class which inherits all the properties and functions from our Animal class by adding Bear : Animal when defining our class
Added other properties more specific to a bear
Used override keyword to define our StateType() and GetMad() methods
Added EatHiker() method
Create a Cat class that inherits from Animal by using public class Cat : Animal
Override GetMad() method to output "Hisss!"
We can also have a class inherit from a subclass such as having PolarBear inherit from Bear
public class PolarBear : Bear
Create some objects and values to demonstrate inheritance.
Challenge
Create a dog base class and make a few subclasses that inherit from your base class and implement it by printing some of the data to your Console.
public class Bear : Animal
{
//PROPERTIES
public bool hibernate { get; set; }
public bool stealsPicnicBaskets { get; set; }
public int numberOfSalmonPerDay { get; set; }
public string temperatureOfPorridge { get; set; } //Hot Cold Just Right
//METHODS
public override void StateType()
{
Console.WriteLine("I am a bear.");
}
public void EatHiker()
{
Console.WriteLine("Man that hiker tasted great.");
}
public override void GetMad()
{
Console.WriteLine("I am a BIG BAD BEAR!!");
}
}
public class Cat : Animal
{
public override void GetMad()
{
Console.WriteLine("Hissss!");
}
}
public class PolarBear : Bear
{
//Can also inherit from subclass
public override void GetMad()
{
Console.WriteLine("I am cold and mad!");
}
}
class Program
{
static void Main(string[] args)
{
//We can create an Object from the base class if the class is not abstract
Animal georgeTheAnimalSteel = new Animal();
georgeTheAnimalSteel.GetMad();
//A few objects built from subclasses of Animal
Bear barryTheBear = new Bear();
barryTheBear.LatinName = "Ursidae";
barryTheBear.GetMad();
barryTheBear.EatHiker();
barryTheBear.numberOfSalmonPerDay = 10;
barryTheBear.StateType();
Cat benvolioTheBoo = new Cat();
Console.ReadLine();
}
}