Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Runtime Polymorphism in Java

    Runtime polymorphism, also known as Dynamic Method Dispatch, is the process through which a call to an overridden method is resolved at runtime rather than compile-time. The method includes using a superclass's reference variable to invoke an overridden method.The method to be called will be determined by the object to which the reference variable refers.

Table Of Content

  • Runtime polymorphism in Java
  • Rules for Run Time Polymorphism
  • Upcasting in java
  • Example of Java Run-Time Polymorphism


Benefits of Runtime Polymorphism in Java

Runtime polymorphism offers a significant advantage by allowing a class to provide its specific implementation to an inherited method. This implementation transfer from one method to another can be carried out without modifying the parent class object's codes. Additionally, a call to a method that has been overridden can be resolved dynamically while in use.





Rules for Run Time Polymorphism

  • The method names should be the same for the parent class and the child class.
  • The parameter for both parent and child class methods should be the same.
  • An IS-A relationship must be established.
  • The parent class's private methods cannot be overridden.
  • Static method overriding is not supported.

Upcasting

Upcasting is the practise of referencing an object from a child class in a reference variable of the parent class.


Syntax

 class  A{}  
 class  B  extendsA{}   

 A a= new   B();//upcasting    

In the example below, two classes Car and Nano are being created. The run() method of the Car class is overridden by the Nano class. Using the Parent class's reference variable, we are invoking the run method. The subclass method is called at runtime because it makes reference to the subclass object and overrides the parent class method.Runtime polymorphism refers to the fact that the JVM, not the compiler, decides which method to invoke.


Example of Java Run-Time Polymorphism
 // Java Run-Time Polymorphism example  
class car 
    { 
        int speedlimit = 25; 
        void run() { 
        System.out.println("Runnning at 25 kmph speed"); 
        } 
    } 
    class Nano extends car 
    { 
        int speedlimit = 35; 
        void run() { 
              System.out.println("Runnning at 35 kmph speed"); 
        } 
        public static void main(String args[]) 
        { 
        car obj = new Nano();  //upcasting
        System.out.println(obj.speedlimit);
        obj.run();
        }
    }
    




Output:

25   // run time polymorphism achive 
Runnning at 35 kmph speed 

Runtime Polymorphism with Multilevel Inheritance

Java program with Runtime Polymorphism and Multilevel Inheritance
class Father{  
    void eat(){
        System.out.println("Father Eating");
    }  
 }  
 class Daughter extends Father{  
    void eat(){
        System.out.println("Daughter Eating");
     }  
 }
 class Son extends Father{  
    void eat(){
        System.out.println("Son Eating");
     }  
 }
class DockerTpoint{  
     public static void main(String args[]){  
         Father father,father2,father3;
         
         father=new Father(); 
         father.eat();
         
         father2=new Daughter();  
         father2.eat();
         
         father3=new Son(); 
         father3.eat();
     }  
 }
 




Output:

Father Eating 
Daughter Eating 
Son Eating 

Runtime Polymorphism Example: Shape

class Shape{  
    void draw(){
	  System.out.println("Drawing ");
  }  
}  
class Rectangle extends Shape{  
  void draw(){
	  System.out.println("Rectangle");
   }  
}  
class Circle extends Shape{  
  void draw(){
	  System.out.println("Circle ");
  }  
}  
class Triangle extends Shape{  
 void draw(){
	 System.out.println("Triangle");
  }  
}  
class Square extends Shape{  
  void draw(){
		 System.out.println("Square");
  }  
} 
class DockerTpoint{  
	public static void main(String args[]){  
		Shape shape;  
		shape=new Rectangle();  
		shape.draw();  
		
		shape=new Circle();  
		shape.draw();
		
		shape=new Triangle();  
		shape.draw(); 
		
		shape=new Square();
		shape.draw();
	}  
}  




Output:

Rectangle 
Circle  
Triangle 
Square 

Java Dynamic Binding Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Difference between Early and Late Binding in Java
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.