How does polymorphism promote extensibility in java




















Real life example of polymorphism: A person at the same time can have different characteristic. Like a man at the same time is a father, a husband, an employee. So the same person posses different behavior in different situations. This is called polymorphism.

Attention reader! 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. The determination of the method to be called is based on the object being referred to by the reference variable. If the reference variable of Parent class refers to the object of Child class, it is known as upcasting.

For example:. For upcasting, we can use the reference variable of class type or an interface type. For Example:. In this example, we are creating two classes Bike and Splendor. Splendor class extends Bike class and overrides its run method. We are calling the run method by the reference variable of Parent class. Since it refers to the subclass object and subclass method overrides the Parent class method, the subclass method is invoked at runtime.

Since method invocation is determined by the JVM not compiler, it is known as runtime polymorphism. Consider a scenario where Bank is a class that provides a method to get the rate of interest. However, the rate of interest may differ according to banks. A method is overridden, not the data members, so runtime polymorphism can't be achieved by data members. In the example given below, both the classes have a data member speedlimit. We are accessing the data member by the reference variable of Parent class which refers to the subclass object.

Since we are accessing the data member which is not overridden, hence it will access the data member of the Parent class always. JavaTpoint offers too many high quality services. Mail us on [email protected] , to get more information about given services.

Please mail your requirement at [email protected] Duration: 1 week to 2 week. Java Training Basics of Java. Abstract class Interface Abstract vs Interface.

Package Access Modifiers Encapsulation. SBI Rate of Interest: 8. Thank you. How is the inheritance pattern between parents and offspring represented dia… Explain how the Chromosomal Theory of Inheritance helped to advance our unde… Explain the difference between an overloaded and an overridden method. Give … What is meant by the term epigenetic inheritance? What are two examples of s… Packaging data attributes and behaviors into a single unit so that the imple… What does the concept of the inheritance of acquired characteristics propose… Do you think that polymorphism in a gene can alter the life history of an or… Problem Discuss three proper ways in which you can assign….

View Full Video Already have an account? Pawan Y. Problem 6 Easy Difficulty How does polymorphism promote extensibility? Answer View Answer. Topics No Related Subtopics. Java How To Program. Discussion You must be signed in to discuss. Top Educators. Recommended Videos Problem 2.



0コメント

  • 1000 / 1000