Why is overloading called compile-time polymorphism?
Have you ever wondered why overloading is called compile-time polymorphism? Read on to unveil the deep secrets of compile-time polymorphism!
Programming polymorphism basically refers to carrying out a particular action in a variety of ways. The name is a combination of Poly and Morphs, two Greek words. Here the word poly means a lot or many, and the word morphs mean forms. Hence, Polymorphism means something having many forms.
What is compile-time polymorphism and why is it a synonym for overloading?
In this article, we will look at the basic concept of Polymorphism and compile-time polymorphism in programming. We will also look at their qualities, method overloading as well as advantages of compile time polymorphism.
The concept of compile-time polymorphism in programming refers to the capacity of any said object to take several forms.
The concept of compile time polymorphism methods depending on the various qualities they possess. Let us say we have an object defined as a class Cell phone.
So, in this case, this class will have,
Properties (general characteristics of the class): material, colour, brand, system, storage, quality, etc.
Methods (which are basically the functions of the class): calling, texting, entertainment, etc.
So, If the above class cell phone is to be polymorphic, then its methods and properties can be called using the same class name but will invoke different types depending on their usage.
There are 2 types of Polymorphism, including compile time polymorphism:
Static Polymorphism (also called compile time polymorphism):
The Polymorphism that can be resolved by the compiler in compiler time is known as static Polymorphism. Method overloading can be considered an example of static Polymorphism.
Method polymorphism overloading enables having one or more than more methods having the same names if the parameters of methods are different in various aspects like sequence, data types, etc. Method overloading helps to enhance the readability of a program or code.
There are 2 ways in which methods can be overloaded easily.
By changing the number of parameters involved:
In this type, method overloading can be done by overloading methods in a function call with a lot of parameters.
When a function is called, the compiler will consider all the numbers of parameters and then decide, based on that data, how the method call will be resolved.
By changing the data types of parameters:
- In this type, method overloading is achieved through overloading methods in a function with different types of parameters.
- When a function is called, the compiler considers the data type of the input parameters and then decides the method call based on that.
Runtime polymorphism (also called dynamic Polymorphism):
- Also known as dynamic dispatch, it is a process wherein resolving a call to an overridden method happens during runtime.
- Also known as dynamic dispatch, it is a process wherein resolving a call to an overridden method happens during runtime. Hence, the name runtime polymorphism!
Difference between Run time and Compile tile polymorphism:
There are a few basic, structural and operational differences between run time polymorphism and compile time polymorphism. The differences are as follows:
In compile time, the call is always resolved by the compiler while in run time, it is not resolved by the compiler.
Compile time polymorphism is achieved by operator and function overloading. Run time polymorphism is achieved by virtual pointers and functions.
Compile time Polymorphism provides fast execution since the method is known beforehand. The execution speed of run time polymorphism is comparatively slower since the method of execution is only known at run time.
The method of Inheritance while using compile-time polymorphism is not allowed. At the same time, it is allowed while using run-time polymorphism.
Method overloading is used by compile-time Polymorphism, whereas method overriding is used by run-time Polymorphism. Multiple methods with the same name that have distinct parameters, return types, etc., are said to be overloading. Contrarily, method overriding merely refers to the use of the same method with identical arguments but with a different class associated with them.
Compile time polymorphism is also known as static binding or overloading. Run time polymorphism is referred to as dynamic binding or overriding.
Advantages of Compile time Polymorphism:
There are many advantages of compile-time polymorphism. Here are a few of them:
A programmer's code can be easily reused with the help of Polymorphism. This means that the classes that have been tested, written and implemented once previously can be used again as and when required.
Since these codes can be reused over and over, it substantially reduces the time required for writing new codes.
Polymorphism reduces coupling between different functionalities.
It is easy to debug the codes with Polymorphism.
A single variable can be used to store multiple data types.
It is also efficient in improving the clarity of the code.
Because it is found early in the compilation process, it has a better and faster implementation time.
Compile-time Polymorphism's inability to utilise inheritance is its only drawback.
DAG in compiler design
Compiler design is a detailed concept including certain other vital concepts. one such DAG concept in compiler design. The Directed Acyclic Graph or DAG represent block structures that help visualise the values between all the basic blocks. it would be helpful in giving you a better understanding on compiler design.
Conclusion:
In conclusion, Compile time Polymorphism is an object's ability to take various forms. The word Polymorphism means anything that possesses several forms. There are many advantages of Polymorphism, like enhanced readability, clarity, reduced processing time and effort of writing the codes.
you should know that there are 2 types of Polymorphism in programming. Namely, run time Polymorphism and compile time Polymorphism.
There are certain differences between them, like processing time, structure and operating methods.
Hence, it can be concluded that compile-time polymorphism is a very useful, efficient and time-effective method of coding, which is excellent for someone who is short on time and needs to reuse some code.
Comments
Post a Comment