Why is polymorphism important




















I can have functions with different name. Why should I try to implement polymorphism in my application. Classic answer: Imagine a base class Shape. It exposes a GetArea method. Imagine a Square class and a Rectangle class, and a Circle class. You don't have to know which exact subclass of Shape you use, you just call GetArea and you get your result, independent of which concrete type is it.

An important thing to notice here is - you don't have to know the exact type of the class you're using, just the base type, and you will get the right result. This is very useful in more complex systems as well. Also it makes processing the list of items more straightforward. Say for example all groceries implements the method price , processing this is easy:. Polymorphism is the foundation of Object Oriented Programming.

It means that one object can be have as another project. So how does on object can become other, its possible through following. One of the main advantage of it is switch implementations. Lets say you are coding an application which needs to talk to a database. And you happen to define a class which does this database operation for you and its expected to do certain operations such as Add, Delete, Modify. So you as programmer, would define an interface that you could use, such as Now you may have multiple implementations, lets say we have one for RDBMS and other for direct file-system.

You can use polymorphism concept in many places, one praticle example would be: lets you are writing image decorer, and you need to support the whole bunch of images such as jpg, tif, png etc. So your application will define an interface and work on it directly. And you would have some runtime binding of various implementations for each of jpg, tif, pgn etc.

One other important use is, if you are using java, most of the time you would work on List interface, so that you can use ArrayList today or some other interface as your application grows or its needs change. Advantage of polymorphism is client code doesn't need to care about the actual implementation of a method. Take look at the following example. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

Polymorphism is considered one of the important features of Object-Oriented Programming. Polymorphism allows us to perform a single action in different ways. In other words, polymorphism allows you to define one interface and have multiple implementations. In Java polymorphism is mainly divided into two types:. Skip to content. Change Language. Related Articles. Basics of Java. Operators in Java.

Packages in Java. Flow Control in Java. Inheritance has to do with methods and functions inheriting the attributes of another class. Polymorphism allows program code to have different meaning or functions while encapsulation is the process of keeping classes private so they cannot be modified by external codes. Begin typing your search term above and press enter to search. Press ESC to cancel. Skip to content Home Philosophy Why do we need polymorphism?

Ben Davis October 15, Why do we need polymorphism? What is the function of polymorphism? Are humans polymorphic? What are the advantages and disadvantages of polymorphism? Which can show polymorphism? Which of the following is incorrect for polymorphism? What is difference between compile time and run time polymorphism? Why it is called compile time polymorphism? Why it is called runtime polymorphism? Why method overriding is called dynamic polymorphism? What is difference between static and dynamic polymorphism?

What is overloading and overriding? Can final method be overloaded? If you talk about a real-life example of polymorphism, it could be a person at the same time can have different characteristics; for example, a man at the same time is a son, a husband, an employee.

So the same person acquires different behavior in different situations. This, in object-oriented terms, is called polymorphism, an important characteristic of object-oriented programming. This has been a guide to What is polymorphism? Here we discuss the Working, Scope, need and advantages of polymorphism.

You can also go through our other suggested articles to learn more —. Submit Next Question. By signing up, you agree to our Terms of Use and Privacy Policy. Forgot Password? This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy.

By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy. What is Polymorphism? Popular Course in this category. Course Price View Course. Free Software Development Course.



0コメント

  • 1000 / 1000