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.
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.
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(); }
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(); }
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.
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(); } }
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 { }
SpacePrivate
and SpaceCadet
abstract classes. This is because the SpacePrivate
abstract class is inheriting from the SpaceCadet
abstract class: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(); } }
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.
3.144.113.55