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.
To illustrate the concept of inheritance, we will create a few classes that inherit from another to form new, more feature-rich objects.
Chapter3
: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.SpaceShip
:public class SpaceShip { }
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.
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 { }
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) { } }
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 { }
Annihilator
class that only belong to this type of SpaceShip
class:public class Annihilator : Destroyer { public void TractorBeam() { } public void PlanetDestructionCapability() { } }
Chapter3
class library by right-clicking on References under the CodeSamples project and selecting Add Reference from the context menu:Chapter3
solution under Projects | Solutions. This will allow you to use the classes we just created in your console application: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: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.
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: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);
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
: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);
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.
3.17.164.34