Using abstraction

With abstraction, we take from the object we want to create the basic functionality that all objects derived from the abstracted object must have. To explain this in simple terms, we abstract the common functionality and put it in a single class that will be used to provide this shared functionality to all classes that inherit from it.

Getting ready

To explain abstraction, we will use abstract classes. Imagine that you are dealing with trainee space astronauts who need to progress through the ranks as they get trained. The truth is that once you as trainee learn a new skill, that skill is learned and will remain with you even though you learn more advanced ways to do things. You must also implement all the previous skills learned in the new object you create. Abstract classes demonstrate this concept very nicely.

How to do it…

  1. Create an abstract class called SpaceCadet. This is the first type of astronaut you can be when starting with training. The abstract class and its members are defined using the abstract keyword. A thing to note is that abstract classes cannot be instantiated. The members represent the skills that SpaceCadet will have, such as negotiation and basic weapons training:
    public abstract class SpaceCadet
    {
        public abstract void ChartingStarMaps();
        public abstract void BasicCommunicationSkill();
        public abstract void BasicWeaponsTraining();
        public abstract void Negotiation();
    }
  2. Next, create another abstract class called SpacePrivate. This abstract class inherits from the SpaceCadet abstract class. What we are basically saying is that when a space cadet is trained as a space private, they will still have all the skills learned as a space cadet:
    public abstract class SpacePrivate : SpaceCadet
    {
        public abstract void AdvancedCommunicationSkill();
        public abstract void AdvancedWeaponsTraining();
        public abstract void Persuader();
    }
  3. To demonstrate this, create a class called LabResearcher and inherit the SpaceCadet abstract class. Inheriting from the abstract class is done by defining a colon and abstract class name after the newly created class name. This tells the compiler that the LabResearcher class inherits from the SpaceCadet class:
    public class LabResearcher : SpaceCadet
    {
        
    }

    Because we are inheriting an abstract class, the compiler will underline the LabResearcher class name to warn us that the derived class does not implement any of the methods in the SpaceCadet abstract class.

  4. If you hover your mouse over the squiggly line, you will see that the lightbulb tip provides us with the issues discovered:
    How to do it…
  5. Visual Studio does a great job of providing a solution to the issues discovered. By typing Ctrl + . (Control key and dot), you can let Visual Studio show you some potential fixes (in this case, only one fix) for the issues identified:
    How to do it…
  6. After Visual Studio has added the required methods, you will see that these are the same methods defined in the SpaceCadet abstract class. Abstract classes, therefore, require the classes inheriting from the abstract class to implement the methods defined in the abstract class. You will also notice that the methods added to the LabResearcher class contain no implementation and will throw an exception if used as is:
    public class LabResearcher : SpaceCadet
    {
        public override void BasicCommunicationSkill()
        {
            throw new NotImplementedException();
        }
    
        public override void BasicWeaponsTraining()
        {
            throw new NotImplementedException();
        }
    
        public override void ChartingStarMaps()
        {
            throw new NotImplementedException();
        }
    
        public override void Negotiation()
        {
            throw new NotImplementedException();
        }
    }
  7. Next, create a class called PlanetExplorer and make this class inherit from the SpacePrivate abstract class. You will remember that the SpacePrivate abstract class inherited from the SpaceCadet abstract class:
    public class PlanetExplorer : SpacePrivate
    {
        
    }
  8. Visual Studio will once again warn you that your new class does not implement the methods of the abstract class that you are inheriting from. Here, however, you will notice that the lightbulb tip informs you that you are not implementing any of the methods in the SpacePrivate and SpaceCadet abstract classes. This is because the SpacePrivate abstract class is inheriting from the SpaceCadet abstract class:
    How to do it…
  9. To fix the issues identified, type Ctrl + . (Control key and dot) and let Visual Studio show you some potential fixes (in this case, only one fix) for the issues identified:
    How to do it…
  10. After the fixes have been added to your code, you will see that the PlanetExplorer class contains all the methods in the SpacePrivate and SpaceCadet abstract classes:
    public class PlanetExplorer : SpacePrivate
    {
        public override void AdvancedCommunicationSkill()
        {
            throw new NotImplementedException();
        }
    
        public override void AdvancedWeaponsTraining()
        {
            throw new NotImplementedException();
        }
    
        public override void BasicCommunicationSkill()
        {
            throw new NotImplementedException();
        }
    
        public override void BasicWeaponsTraining()
        {
            throw new NotImplementedException();
        }
    
        public override void ChartingStarMaps()
        {
            throw new NotImplementedException();
        }
    
        public override void Negotiation()
        {
            throw new NotImplementedException();
        }
    
        public override void Persuader()
        {
            throw new NotImplementedException();
        }
    }

How it works…

Abstraction has allowed us to define a common set of functionality that is to be shared among all the classes that derive from the abstract classes. The difference between inheriting from the abstract class and a normal class is that with an abstract class, you have to implement all the methods defined in that abstract class.

This makes the class easy to version and change. If you need to add new functionality, you can do so by adding that functionality to the abstract class without breaking any of the existing code. Visual Studio will require that all inherited classes implement the new method defined in the abstract class.

You can, therefore, be assured that the change applied will be implemented in all your classes that derive from the abstract classes in your code.

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

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