What is polymorphism in programming




















See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Skip to content. Change Language. Related Articles. Object Oriented Programming. Exception Handling. File Handling. Table of Contents. Save Article. Looking at the greek roots of the term polymorphism can help to further clarify the similarities between these definitions.

The word poly means "many" and the word morph means "form" , so when we talk about a polymorphism we're talking about something that appears in many different forms. This brings us to our computer science definition of polymorphism - we'll offer three different versions for maximum clarity.

Polymorphism is one of the core characteristics of any object-oriented programming language. Polymorphic code allows a program to process objects differently depending on their data type or class, with the ability to redefine methods for derived classes. Without getting too deep into specifics of how to write polymorphic code given that it can be done differently depending on which language you use , let's review two of the most common examples used to illustrate polymorphism in computer science.

Polymorphism in programming gives a program the ability to redefine methods for derived classes. Imagine that we write a piece of code where we define a base class called shape.

We may want to define specific methods such as area and perimeter that can be applied to our base class - but there's a problem.

Our knowledge of geometry tells us that we cannot use the same method to compute the area and perimeter of both a circle and a rectangle.

In particular, the area and perimeter of a circle depend on its radius and the area and perimeter of a rectangle depending on its width and length. To solve this problem using polymorphism coding, we could define a circle and rectangle as sub-classes or derived classes of the base class shape.

Each sub-class can have its own method for area and perimeter and each method will accept different parameters - length and width for rectangles, radius for circles. Now our base class shape has two derived classes - rectangle and circle - each with its own methods for area and perimeter. When we call one of the methods, the provided set of parameters can be used to determine which method should be called.

If we provide a single parameter for the radius, our program can call the methods that belong to the class circle. If we provide two parameters for length and width, our program can call the methods that belong to the class rectangle. The type of the reference, which you can see in your code, is irrelevant. You need to distinguish three general scenarios:. The first scenario is pretty simple.

So, you can only brew a CoffeeSelection. The second scenario is similar. This is the most interesting scenario and the main reason why I explain dynamic polymorphism in such details. It just looks like a BasicCoffeeMachine. But if you call the brewCoffee method on the coffeeMachine variable, the JVM knows that it is an object of type PremiumCoffeeMachine and executes the overridden method.

This is called late binding. Polymorphism is one of the core concepts in OOP languages. It describes the concept that different classes can be used with the same interface. Each of these classes can provide its own implementation of the interface. Java supports two kinds of polymorphism. You can overload a method with different sets of parameters. This is called static polymorphism because the compiler statically binds the method call to a specific method.

Within an inheritance hierarchy, a subclass can override a method of its superclass. If you instantiate the subclass, the JVM will always call the overridden method, even if you cast the subclass to its superclass. What is Polymorphism? Example The classic example is of the Shape class and all the classes that are inherited from it, such as: Rectangle Triangle Circle.

Below is an example of Polymorphism.



0コメント

  • 1000 / 1000