Home > Java > javaTutorial > body text

Runtime Polymorphism in Java

王林
Release: 2024-08-30 15:43:45
Original
579 people have browsed it

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 Tests

For runtime polymorphism in Java, you should follow the basic syntax of java with annotations.

@Override
Copy after login

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
}
}
Copy after login

Output:

Runtime Polymorphism in Java

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
}
}
Copy after login

Output:

Runtime Polymorphism in Java

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
}
}
Copy after login

Output:

Runtime Polymorphism in Java

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!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template