- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Using run-time polymorphism in Java
A single action can be performed in multiple ways using the concept of polymorphism. Run-time polymorphism can be performed by method overriding. The overridden method in this is resolved at compile time.
A program that demonstrates run-time polymorphism in Java is given as follows:
Example
class Animal { void sound() { System.out.println("Animal makes sound"); } } class Cat extends Animal { void sound() { System.out.println("Cat Meows"); } } class Dog extends Animal { void sound() { System.out.println("Dog Barks"); } } class Cow extends Animal { void sound() { System.out.println("Cow Moos"); } } public class Demo { public static void main(String[] args) { Animal a; a = new Cat(); a.sound(); a = new Dog(); a.sound(); a = new Cow(); a.sound(); } }
Output
Cat Meows Dog Barks Cow Moos
- Related Articles
- What is run time polymorphism in C#?
- What is the difference between compile time polymorphism and runtime polymorphism in java?
- Run-time Stack mechanism in Java
- Difference between compile-time polymorphism and runtime polymorphism
- Polymorphism in Java
- What is Java run time environment?
- What is compile time polymorphism in C#?
- Runtime Polymorphism in Java
- What are Java JVM Run-time Data Areas?
- What is the difference between compile time errors and run time errors in Java?
- Explain Compile time and Run time initialization in C programming?
- Java Runtime Polymorphism with multilevel inheritance
- RTTI (Run-time type Information) in C++
- Dynamic method dispatch or Runtime polymorphism in Java
- Are Generics applied at compile time or run time?

Advertisements