Stage 3: Another Abstract class

Stage 3: Another Abstract class

Highlights:

  • The fly method needs to be better provisioned to accommodate future birds without repeating the code.

  • A new class called "bird" is introduced to model a hierarchy where all birds inherit from "animal".

  • The bird class has a constructor that accepts the name of the bird and passes it to the parent constructor.

  • The bird class is marked as abstract, as the climb method is also an abstract concept like animal.

  • Multi-level inheritance is used, with the animal class inherited by the bird class, which is inherited by the parrot class.

  • The climb contract is specified in the animal and concretely implemented in the parrot.

  • The fly method is only in the bird class, so any bird can inherit it, and repetition is avoided.

Animal.cs

No change!

Bird.cs

namespace MyApplication.Animals;

public abstract class Bird(string name) : Animal(name)
{
    //we dont know how the climb actly looks
    
    public void Fly()
    {
        Console.WriteLine($"{Name} is Flying!");
    }
}

Cat.cs

No change!

Dog.cs

No change!

Parrot.cs

namespace MyApplication.Animals;

public class Parrot(string name) : Bird(name)
{    
    public override void Climb()
    {
        Console.WriteLine($"{Name} is climbing on a tree!");
    }    
}

Program.cs

No change!

Last updated