Implementing polymorphism

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.

Getting ready

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.

How to do it…

  1. Create an abstract class called 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();
    }
  2. Next, create a class called 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
    {
        
    }
  3. Visual Studio will underline the 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:
    How to do it…
  4. 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…
  5. Visual Studio then adds the missing implementation to your 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();
        }
    }
  6. Create another class called RoscosmosShuttle and inherit from the same Shuttle abstract class:
    public class RoscosmosShuttle : Shuttle
    {
    
    }
  7. Visual Studio will underline the 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:
    How to do it…
  8. 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…
  9. The overridden method is then added to the 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();
        }
    }
  10. To see an example of static polymorphism, create the following overloaded constructor for 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)
    {
    
    }

How it works…

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.

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

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