Using inheritance in C#

In today's world, inheritance is usually associated with the end of things. In OOP, however, it is associated with the beginning of something new and better. When we create a new class, we can take an already existing class and have our new class inherit from it. This means that our new object will have all the features of the inherited class, as well as the additional features added to the new class. This is at the root of inheritance. We call a class that inherits from another a derived class.

Getting ready

To illustrate the concept of inheritance, we will create a few classes that inherit from another to form new, more feature-rich objects.

How to do it…

  1. Create a new class library by right-clicking on your solution and selecting Add and then New Project from the context menu:
    How to do it…
  2. From the Add New Project dialog screen, select Class Library from the installed templates and call your class Chapter3:
    How to do it…
  3. Your new class library will be added to your solution with a default name Class1.cs, which we renamed to Recipes.cs in order to distinguish the code properly. You can, however, rename your class to whatever you like if it makes more sense to you.
  4. To rename your class, simply click on the class name in the Solution Explorer and select Rename from the context menu:
    How to do it…
  5. Visual Studio will ask you to confirm the renaming of all references to the code element Class1 in the project. Just click on Yes:
    How to do it…
  6. Now, let's create a new class called SpaceShip:
    public class SpaceShip
    {
    
    }
  7. Our SpaceShip class will contain a few methods that describe the basics of a spaceship. Go ahead and add these methods to your SpaceShip class:
    public class SpaceShip
    {
        public void ControlBridge()
        {
    
        }
        public void MedicalBay(int patientCapacity)
        {
    
        }
        public void EngineRoom(int warpDrives)
        {
    
        }
        public void CrewQuarters(int crewCapacity)
        {
    
        }
        public void TeleportationRoom()
        {
    
        }
    }

    Because the SpaceShip class forms part of all other intergalactic vessels, it becomes the blueprint for every other vessel.

  8. Next, we want to create a Destroyer class. To accomplish this, we will create a Destroyer class and use a colon after the class name to indicate that we want to inherit from another class (the SpaceShip class). Therefore, the following needs to be added when creating the Destroyer class:
    public class Destroyer : SpaceShip
    {
    
    }

    Note

    We can also say that the Destroyer class is derived from the SpaceShip class. The SpaceShip class is therefore the base class of all other intergalactic vessels.

  9. Next, add a few methods to the Destroyer class that are unique to a destroyer. These methods belong only to the Destroyer class and not to the SpaceShip class:
    public class Destroyer : SpaceShip
    {
        public void WarRoom()
        {
    
        }
        public void Armory(int payloadCapacity)
        {
    
        }
    
        public void WarSpecialists(int activeBattalions)
        {
    
        }
    }
  10. Finally, create a third class called Annihilator. This is the most powerful intergalactic vessel and is used to wage war on planets. Let the Annihilator class inherit from the Destroyer class by creating the class and marking it as derived from the Destroyer class as follows Annihilator : Destroyer:
    public class Annihilator : Destroyer
    {
        
    }
  11. Finally, add a few methods to the Annihilator class that only belong to this type of SpaceShip class:
    public class Annihilator : Destroyer
    {
        public void TractorBeam()
        {
    
        }
    
        public void PlanetDestructionCapability()
        {
    
        }
    }
  12. Inside the console application, add a reference to the Chapter3 class library by right-clicking on References under the CodeSamples project and selecting Add Reference from the context menu:
    How to do it…
  13. In the Reference Manager window, select the Chapter3 solution under Projects | Solutions. This will allow you to use the classes we just created in your console application:
    How to do it…
  14. What we see now is that when we create a new instance of the SpaceShip class, only the methods defined in that class are available to us. This is because the SpaceShip class does not inherit from any other class:
    How to do it…
  15. Go ahead and create the SpaceShip class with its methods in the console application:
    SpaceShip transporter = new SpaceShip();
    transporter.ControlBridge();
    transporter.CrewQuarters(1500);
    transporter.EngineRoom(2);
    transporter.MedicalBay(350);
    transporter.TeleportationRoom();

    You will see that these are the only methods available to us when instantiating a new instance of this class.

  16. Next, create a new instance of the Destroyer class. You will notice that the Destroyer class contains more methods than what we defined when we created the class. This is because the Destroyer class is inheriting the SpaceShip class and therefore inherits the methods of the SpaceShip class:
    How to do it…
  17. Go ahead and create the Destroyer class with all its methods in the console application:
    Destroyer warShip = new Destroyer();
    warShip.Armory(6);
    warShip.ControlBridge();
    warShip.CrewQuarters(2200);
    warShip.EngineRoom(4);
    warShip.MedicalBay(800);
    warShip.TeleportationRoom();
    warShip.WarRoom();
    warShip.WarSpecialists(1);
  18. Finally, create a new instance of the Annihilator class. This class contains all the methods of the Destroyer class as well as the methods from the SpaceShip class. This is because Annihilator inherits from Destroyer, which, in turn, inherits from SpaceShip:
    How to do it…
  19. Go ahead and create the Annihilator class with all its methods in the console application:
    Annihilator planetClassDestroyer = new Annihilator();
    planetClassDestroyer.Armory(12);
    planetClassDestroyer.ControlBridge();
    planetClassDestroyer.CrewQuarters(4500);
    planetClassDestroyer.EngineRoom(7);
    planetClassDestroyer.MedicalBay(3500);
    planetClassDestroyer.PlanetDestructionCapability();
    planetClassDestroyer.TeleportationRoom();
    planetClassDestroyer.TractorBeam();
    planetClassDestroyer.WarRoom();
    planetClassDestroyer.WarSpecialists(3);

How it works…

We can see that inheritance allowed us to easily extend our classes by reusing functionality that already exists within another class created earlier. You also need to be aware though that any changes to the SpaceShip class will be inherited up the stack to the top-most derived class.

Inheritance is a very powerful feature of C#, which allows developers to write less code and reuse working and tested methods.

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

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