How to do it...

  1. If you haven't already done so in the previous recipe, create an abstract class called Cat:
        public abstract class Cat 
{
public abstract void Eat();
public abstract void Hunt();
public abstract void Sleep();
}
  1. Next, add a class called Cheetah that inherits from the Cat abstract class:
        public class Cheetah : Cat 
{

}
  1. As soon as you inherit from the Cat abstract class, Visual Studio will show you a warning via the lightbulb feature. As you inherited from the abstract class Cat, you have to implement the abstract members within the abstract class in your derived class Cheetah:
  1. This is easily fixable by typing Ctrl +. (period) and fixing all occurrences in the document. You can also do this for the project or solution. For our purpose, we only select the Document link at the bottom of the lightbulb suggestions. Visual Studio will automatically add the abstract methods defined in the abstract class to implement inside your Cheetah class:
  1. You will notice that Visual Studio adds just the methods you need to override but will throw NotImplementedException if you try to use the class as is. The reason for using an abstract class is to implement the functionality defined in the abstract class Cat in the derived class Cheetah. Not doing so contravenes the rules for using abstract classes:
        public class Cheetah : Cat 
{
public override void Eat()
{
throw new NotImplementedException();
}

public override void Hunt()
{
throw new NotImplementedException();
}

public override void Sleep()
{
throw new NotImplementedException();
}
}
  1. To add some implementation, modify your Cheetah class as follows. The implementation in the overridden methods is simple, but this validates the rule of writing some sort of implementation in the overridden methods:
        public class Cheetah : Cat 
{
public override void Eat()
{
WriteLine($"The cheetah eats.");
}

public override void Hunt()
{
WriteLine($"The cheetah hunts.");
}

public override void Sleep()
{
WriteLine($"The cheetah sleeps.");
}
}
You will notice that the WriteLine method is used without the Console class. This is because we are using a new feature introduced in C# 6.0 that allows developers to bring static classes into scope by adding the using static System.Console; statement to the top of your class file.
  1. Create an interface called IPurrable that will be implemented in the Cheetah class. A common naming convention for interfaces dictates that the interface name should be prefixed with a capital I:
        interface IPurrable 
{

}
  1. Next, we will add a method to the interface that any class implementing the interface must implement. You will notice that the interface's SoftPurr method contains no implementation at all. It, however, specifies that we will need to pass this method an integer value for the decibel that the Cheetah class will purr at:
        interface IPurrable 
{
void SoftPurr(int decibel);
}
  1. The next step is to implement the IPurrable interface in the Cheetah class. To do this, we need to add the IPurrable interface name after the Cat abstract class name. If the Cheetah class did not inherit from the abstract class, then the interface name would simply follow after the colon:
        public class Cheetah : Cat, IPurrable 
{
public override void Eat()
{
WriteLine($"The cheetah eats.");
}

public override void Hunt()
{
WriteLine($"The cheetah hunts.");
}

public override void Sleep()
{
WriteLine($"The cheetah sleeps.");
}
}
  1. After specifying that the Cheetah class implements the IPurrable interface, Visual Studio once again displays a warning via the lightbulb feature. It is warning us that the Cheetah class does not implement the SoftPurr method defined in the interface IPurrable:
  1. As we did earlier, we can let Visual Studio suggest possible fixes for the problems encountered by typing Ctrl + . (period). Visual Studio suggests that the interface can be implemented implicitly or explicitly:
  1. Knowing when to use an implicit or explicit implementation is also quite easy. We first need to know when using one over the other would be preferred. Let's start off by implementing the SoftPurr method implicitly by selecting the first option in the lightbulb suggestion. You will see that this uses the SoftPurr method defined in the IPurrable interface as if it were part of the Cheetah class:
        public class Cheetah : Cat, IPurrable 
{
public void SoftPurr(int decibel)
{
throw new NotImplementedException();
}

public override void Eat()
{
WriteLine($"The cheetah eats.");
}

public override void Hunt()
{
WriteLine($"The cheetah hunts.");
}

public override void Sleep()
{
WriteLine($"The cheetah sleeps.");
}
}
  1. If we look at the SoftPurr method, it looks like a normal method inside the Cheetah class. This would be fine unless our Cheetah class already contains a property called SoftPurr. Go ahead and add a property called SoftPurr to your Cheetah class:
        public class Cheetah : Cat, IPurrable 
{
public int SoftPurr { get; set; }

public void SoftPurr(int decibel)
{
throw new NotImplementedException();
}

public override void Eat()
{
WriteLine($"The cheetah eats.");
}

public override void Hunt()
{
WriteLine($"The cheetah hunts.");
}

public override void Sleep()
{
WriteLine($"The cheetah sleeps.");
}
}
  1. Visual Studio immediately displays a warning by telling us that the Cheetah class already contains a definition for SoftPurr:
  1. It is here that the use of an explicit implementation becomes evident. This specifies that the SoftPurr method is a member of the implementation defined in the IPurrable interface:
  1. Therefore, selecting the second option to implement the interface explicitly will add the SoftPurr method to your Cheetah class as follows:
        public class Cheetah : Cat, IPurrable 
{
public int SoftPurr { get; set; }

void IPurrable.SoftPurr(int decibel)
{
throw new NotImplementedException();
}

public override void Eat()
{
WriteLine($"The cheetah eats.");
}

public override void Hunt()
{
WriteLine($"The cheetah hunts.");
}

public override void Sleep()
{
WriteLine($"The cheetah sleeps.");
}
}

The compiler now knows that this is an interface that is being implemented and is therefore a valid line of code.

  1. For the purposes of this book, let's just use the implicit implementation. Let's write some implementation for the SoftPurr method and use the new nameof keyword (introduced in C# 6.0) as well as the interpolated string for the output. Also, remove the SoftPurr property added earlier:
        public void SoftPurr(int decibel) 
{
WriteLine($"The {nameof(Cheetah)} purrs at {decibel} decibels.");
}
  1. Heading over to our console application, we can call our Cheetah class as follows:
        Cheetah cheetah = new Cheetah(); 
cheetah.Hunt();
cheetah.Eat();
cheetah.Sleep();
cheetah.SoftPurr(60);
ReadLine();
  1. Running the application will produce the following output:
..................Content has been hidden....................

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