# 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`&#x20;
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&#x20;
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

```csharp
 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

```csharp
 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!"`

```csharp
 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`

```csharp
 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.&#x20;

```csharp
 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](https://github.com/ElevenfiftyAcademy/DotNet-101-151-CSharpFundamentalsApps/tree/573d3901a9bdae2aaa8318a68f6741c32d5f3bca/docs/Part5/.md/README.md)

## Further Reading

Learn more about [inheritance.](https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/inheritance)

[Next:](/dotnet-101-csharpfundamentals/part-5-inheritance-more-on-object-oriented-programming/5.1-polymorphism.md) Polymorphism


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://eleven-fifty-academy.gitbook.io/dotnet-101-csharpfundamentals/part-5-inheritance-more-on-object-oriented-programming/5.0-inheritance.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
