
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Runtime Polymorphism in Java
Method overriding is an example of runtime polymorphism. In method overriding, a subclass overrides a method with the same signature as that of in its superclass. During compile time, the check is made on the reference type. However, in the runtime, JVM figures out the object type and would run the method that belongs to that particular object.
Example
See the example below to understand the concept −
class Animal { public void move() { System.out.println("Animals can move"); } } class Dog extends Animal { public void move() { System.out.println("Dogs can walk and run"); } } public class TestDog { public static void main(String args[]) { Animal a = new Animal(); // Animal reference and object Animal b = new Dog(); // Animal reference but Dog object a.move(); // runs the method in Animal class b.move(); // runs the method in Dog class } }
Output
This will produce the following result −
Animals can move Dogs can walk and run
- Related Articles
- Java Runtime Polymorphism with multilevel inheritance
- Dynamic method dispatch or Runtime polymorphism in Java
- What is the difference between compile time polymorphism and runtime polymorphism in java?
- Difference between compile-time polymorphism and runtime polymorphism
- Virtual Functions and Runtime Polymorphism in C++
- What is runtime polymorphism or dynamic method overloading?
- Polymorphism in Java
- Using run-time polymorphism in Java
- What is overriding and overloading under polymorphism in java?
- Polymorphism in Python
- How to handle the Runtime Exception in Java?
- What is the Java Runtime Environment (JRE)?
- What is the purpose of Runtime class in Java?
- What is the importance of Runtime class in Java?
- Polymorphism example in C++

Advertisements