To understand polymorphism completely, you should understand inheritance and interfaces as well. You have an IS-A relationship through inheritance (using the extends keyword in the class signature), or through interfaces (using the implements keyword in the class signature). Explanation with CodeĪny object that has an IS-A relationship is considered polymorphic. Polymorphism helps us to create more universal interfaces. It can be achieved by method overloading. This type of polymorphism in Java is also called static polymorphism or static method dispatch. You can see in the image below how this has been done. There are two main types of polymorphism in Java. We can make the socket interface polymorphic by creating different shaped slits. Life would be much easier for everyone if we had sockets that could accept many different types of plugs. To look at the problem another way, generally the issue is we have a socket interface which accepts only 1 type of plug object! Sockets are not polymorphic. The solution most people take is to buy a universal plug adapter. Some have 2 pins, some have 3 pins, some pins are circular, some pins are rectangular, and the configuration of the pins vary. Photo by Call Me Fredīizarrely, there are approximately 16 different types of electrical sockets worldwide. Otherwise, you may not be able to charge your phone and other devices. If you have ever travelled internationally, one item on your packing checklist is likely to be an electrical plug adapter. Let's explain what we mean by this exactly. Polymorphism literally means “ many forms”. This reduces duplication of code when you want the same actions to be performed on different types of objects. Polymorphism allows objects to be treated in a substitutable way.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |