Runtime Polymorphism in Java
In this article, we are going to learn about Runtime Polymorphism in Java. “Poly” means “many”, and “morph” means “type”. So the term polymorphism indicates the same thing of different types. Here we will see how Java archives polymorphism in run time, which means, after compilation but before running of the code.
Syntax:
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsFor runtime polymorphism in Java, you should follow the basic syntax of java with annotations.
@Override
the annotation may be used here to point out which method we want to override specifically.
How does Runtime Polymorphism work in Java?
Runtime polymorphism works in Java by method overriding. Method overriding happens when objects have the same method name and arguments and type as of their parent class but with different functionality. If a child class has that type of method in it, we call it an overridden method.
Why is it called Runtime Polymorphism?
when we call an overridden method of child class through its parent type reference (this phenomenon in java is referred to as “Upcasting”), then the type of the object indicates which method or functionality will be invoked. Making of this decision happens during runtime by JVM after the compilation of code. Hence it is called as Run time polymorphism.
It is also referred to as “Dynamic method dispatch”. Reason being named so, due to the fact that the functionality of the method is dynamically decided in run time as per the object by JVM
It is also called “Late binding” because binding of method and object, which means the functionality of which object’s method will be displayed, is decided late, i.e. after compilation.
Rules and Limitations in Runtime Polymorphism
Below are some of the rules and limitations of runtime polymorphism:
Rules of Runtime Polymorphism
- Methods of child and parent class must have the same name.
- Methods of child and parent class must have the same parameter.
- IS-A relationship is mandatory (inheritance).
Limitations of Runtime Polymorphism
- One cannot override the private methods of a parent class.
- One cannot override Final methods.
- One cannot override static methods.
Examples of Runtime Polymorphism in Java
We will discuss some code examples of Run time polymorphism here.
Example# 1
In this example, we will show how the method showcase() is displaying different messages depending on which type of object it is associated with. When it is associated with the “Parents” type, it is showing messages from a parent class. When it is associated with the “Children” type, it shows messages from the child class.
Code:
class Parents { public void showcase () { System.out.println("I am Parent"); } } class Children extends Parents { @Override public void showcase () { System.out.println("I am Children"); } } public class RunTimePolymorphism { public static void main(String args[]) { Parents superObject = new Parents(); superObject.showcase(); //method of super class or parent class is called Parents subObject = new Children(); // upcasting subObject.showcase();//method of sub class or child class is called by Parent reference, this is called "Run time Polymorphism" Children subObject2 = new Children(); subObject2.showcase(); //method of sub class or child class is called } }
Output:
Example# 2
Let us take an example of run time polymorphism in the case of multilevel inheritance. In this example, we have taken two levels of inheritance into account. In this example, we will show how the method sip() is displaying different messages depending on which type of object it is associated with. When it is associated with the “Human” type, it is showing messages from a parent class. When it is associated with the “Man” type, it shows messages from its child class. Again in the second level of inheritance, when associated with the “Baby” type, it shows messages from its child class of its parent, the “Man” class.
Code:
class Human{ void sip() { System.out.println("Human is sipping"); } } class Man extends Human{ void sip(){ System.out.println("Man is sipping soup"); } } class Baby extends Man{ void sip(){ System.out.println("Baby is sipping milk"); } } public class RunTimePolymorphism { public static void main(String args[]){ Human superObject=new Human(); Human subObject=new Man(); // // upcasting : first level of heritance Human babyObject=new Baby(); // // upcasting : second level of heritance superObject.sip(); subObject.sip(); //run time polymorphism happening in first level of heritance babyObject.sip(); //run time polymorphism happening in second level of heritance } }
Output:
Example# 3
Let us take another example of run time polymorphism in the case of multilevel inheritance. In this example, we have three levels of inheritance is taken into account. In this example, we will show how the method feature () is displaying different features depending on which type of object it is associated with. When it is associated with the “operating system” type, it is showing messages from a parent class. When it is associated with the “DOS” type, it shows messages from its child class. Again in the second level of inheritance, when associated with the “Windows” type, it shows messages from its child class of its parent, the “DOS” class. Again in the third level of inheritance, when associated with the “WindowsMobile” type, it is showing messages from its child class of its parent, the “Windows” class.
Code:
class OperatingSytem{ void feature() { System.out.println("This is Operating Sytem"); } } class DOS extends OperatingSytem{ void feature(){ System.out.println("This is DOS"); } } class Windows extends DOS{ void feature(){ System.out.println("This is Windows"); } } class WindowsMobile extends Windows{ void feature(){ System.out.println("This is Windows Mobile"); } } public class RunTimePolymorphism { public static void main(String args[]){ OperatingSytem superObject=new OperatingSytem(); OperatingSytem subObject=new DOS(); // child object type : first level of heritance OperatingSytem sub2Object=new Windows(); // child object type : second level of heritance OperatingSytem sub3Object=new WindowsMobile(); // child object type : third level of heritance superObject.feature(); subObject.feature(); //run time polymorphism happening in first level of heritance sub2Object.feature(); //run time polymorphism happening in second level of heritance sub3Object.feature(); //run time polymorphism happening in third level of heritance } }
Output:
Conclusion
This concludes our learning of the topic “Runtime Polymorphism in Java”. Write yourself the codes mentioned in the above examples in the java compiler and verify the output. Learning of codes will be incomplete if you will not write code by yourself.
The above is the detailed content of Runtime Polymorphism in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.
