Interface polymorphism

An interface defines the signature that a class must implement. In the PetAnimal example, imagine that we define pet food as providing an amount of energy, as follows:

public interface IPetFood
{
int Energy { get; }
}

By itself, the interface cannot be instantiated but describes what an instance of IPetFood must implement. For example, Kibble and Fish might provide different levels of energy, as shown in the following code:

public class Kibble : IPetFood
{
public int Energy => 7;
}

public class Fish : IPetFood
{
int IPetFood.Energy => 8;
}

In the preceding code snippet, Kibble provides less energy than Fish.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.23.127.197