5.0 Inheritance

File Location

  1. Right click on your solution

  2. Go to Add > New Project and select Console App (.NET Framework)

  3. Name it Inheritance

  4. 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.

  1. First lets make our base class, the class the rest of our animal classes will inherit from

  2. We make a field that will hold the data for our _latinName

  3. Make a property that gets the _latinName and returns based on the condition

  4. Make other properties that make up your basic animal

  5. Make methods that make up your basic animal

 public class Animal
    {
        //Fields
        string _latinName;

        //Properties - get() & set() properties

        public string LatinName
        {
            get
            {
                return _latinName;
            }

            set
            {
                if (String.IsNullOrWhiteSpace(value))
                {
                    throw new ArgumentException("Name can't be empty");
                }
                else
                {
                    _latinName = value.Trim();
                }
            }
        }
        public string Habitat { get; set; }

        public int NumberOfLegs { get; set; }
        public int Weight { get; set; }
        public int AverageNumOffspring { get; set; }

        public bool HasFur { get; set; }
        public bool HasWings { get; set; }
        public bool CanFly { get; set; }

        //Methods - execute some function
        //Need to discuss override-ability that virtual keyword gives.

        public virtual void StateType()
        {
            Console.WriteLine("I am an animal");
        }

        public virtual void GetMad()
        {
            Console.WriteLine("ROAR!!");
        }
    }
  1. 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

  2. Added other properties more specific to a bear

  3. Used override keyword to define our StateType() and GetMad() methods

  4. Added EatHiker() method

 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!!");
        }
    }
  1. Create a Cat class that inherits from Animal by using public class Cat : Animal

  2. Override GetMad() method to output "Hisss!"

 public class Cat : Animal
    {
        public override void GetMad()
        {
            Console.WriteLine("Hissss!");
        }
    }
  1. We can also have a class inherit from a subclass such as having PolarBear inherit from Bear

  2. public class PolarBear : Bear

 public class PolarBear : Bear
    {
        //Can also inherit from subclass
        public override void GetMad()
        {
            Console.WriteLine("I am cold and mad!");
        }
    }
  1. Create some objects and values to demonstrate inheritance.

 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();
        }
    }

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.

Answers

Further Reading

Learn more about inheritance.

Next: Polymorphism

Last updated