
- 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
Java Runtime Polymorphism with multilevel inheritance
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.
We can override a method at any level of multilevel inheritance. See the example below to understand the concept −
Example
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"); } } class Puppy extends Dog { public void move() { System.out.println("Puppy can move."); } } public class Tester { public static void main(String args[]) { Animal a = new Animal(); // Animal reference and object Animal b = new Puppy(); // Animal reference but Puppy object a.move(); // runs the method in Animal class b.move(); // runs the method in Puppy class } }
Output
Animals can move Puppy can move.
- Related Articles
- Runtime Polymorphism in Java
- Multilevel inheritance in Java
- Creating a Multilevel Inheritance Hierarchy in Java
- Dynamic method dispatch or Runtime polymorphism in Java
- Difference between compile-time polymorphism and runtime polymorphism
- What is the difference between compile time polymorphism and runtime polymorphism in java?
- C# Example for MultiLevel Inheritance
- MultiLevel Inheritance in Dart Programming
- Difference Between Inheritance and Polymorphism
- Virtual Functions and Runtime Polymorphism in C++
- What is runtime polymorphism or dynamic method overloading?
- Polymorphism in Java
- Object Serialization with inheritance in Java
- Object Serialization with Inheritance in Java Programming
- Inheritance in Java

Advertisements