Virtual Properties

Like method, properties can also be virtualized. Let's create another class called FastInvader. Don't forget to create a constructor and inherit it from Invader class.

class FastInvader : Invader
{
    public FastInvader(Path path) : base(path)
    { }
}

The behavior of FastInvader is it will move further than normal Invader. We could do the same like DecreasedHealth method earlier. Unfortunately, we will learn something new now.

// in Invader class
protected virtual int stepSize { get; } = 1;

Copy it to FastInvader class.

protected override int stepSize { get; } = 2;

That is the power of encaspsulation, inheritance and polymorphism principles in OOP. We can create another modified object with different attributes and behaviors from its origin object.

results matching ""

    No results matching ""