Polymorphism is a concept that is quite easy to grasp once you have looked at and understood the other pillars of OOP. Polymorphism literally means that something can have many forms. This means that from a single interface, you can create multiple implementations.
There are two subsections to this, namely static and dynamic polymorphism. With static polymorphism, you are dealing with the overloading of methods and functions. You can use the same method, but perform many different tasks.
With dynamic polymorphism, you are dealing with the creation and implementation of abstract classes. These abstract classes act as a blueprint that tells you what a derived class should implement. The following section looks at both.
We will begin by illustrating the use of an abstract class, which is an example of dynamic polymorphism. We will then create overloaded constructors as an example of static polymorphism.
Shuttle
and give it a member called TWR
, which is the calculation of the TWR of the shuttle:public abstract class Shuttle { public abstract double TWR(); }
NasaShuttle
and have it inherit from the abstract class Shuttle
, by putting the abstract class name after a colon at the end of the NasaShuttle
class declaration:public class NasaShuttle : Shuttle { }
NasaShuttle
class because you have told the compiler that the class inherits from an abstract class, but you have not yet implemented the members of that abstract class:NasaShuttle
class. By default, it will add it as not implemented, because you are required to provide implementation for the abstract member you overrode in the abstract class:public class NasaShuttle : Shuttle { public override double TWR() { throw new NotImplementedException(); } }
RoscosmosShuttle
and inherit from the same Shuttle
abstract class:public class RoscosmosShuttle : Shuttle { }
RoscosmosShuttle
class because you have told the compiler that the class inherits from an abstract class, but you have not yet implemented the members of that abstract class:RoscosmosShuttle
class as not implemented. You have just seen an example of dynamic polymorphism in action:public class RoscosmosShuttle : Shuttle { public override double TWR() { throw new NotImplementedException(); } }
NasaShuttle
. The constructor name stays the same, but the signature of the constructor changes, which makes it overloaded:public NasaShuttle(double engineThrust, double totalShuttleMass, double gravitationalAcceleration) { } public NasaShuttle(double engineThrust, double totalShuttleMass, double planetMass, double planetRadius) { }
Polymorphism is something you will easily be using already by simply applying good object oriented principles to the design of your classes. With the abstract Shuttle
class, we saw that the class took on the shape of the NasaShuttle
class and the RoscosmosShuttle
class when it was used to derive those new classes from its abstraction. The constructor of the NasaShuttle
class was then overridden to provide the same method name, but implemented using different signatures.
This is at the heart of polymorphism. Most likely, you have been using it without knowing about it.
3.137.183.10