- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
What is overriding in Java can explain briefly with an example?
If superclass and subclass have methods with same name including parameters. JVM calls the respective method based on the object used to call the method. i.e. if the current object using which the method is called, is of superclass type the method of the superclass is executed.
Or, if the object is of the type subclass then the method of the sub class is executed.
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"); } } 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
Animals can move Dogs can walk and run
- Related Articles
- What is Inheritance in Java? Explain with an example
- What is shallow copy? Explain with an example in Java.
- What is deep copy? Explain with an example in Java.
- What is an anonymous array and explain with an example in Java?
- What is Symbiosis? Explain with an example.
- What is heat? Explain with an example.
- What is PET? Explain briefly.
- What is CheckBoxTreeItem in JavaFX explain with an example?
- What is the use of StringBuffer class can anyone explain with an example?
- What are jagged arrays and explain with an example in Java?
- What is spindle fibre? Explain briefly.
- What is Image Array? Explain with an example in C++
- What is Bubble Sort in Python? Explain with an example?
- What is the character count? Explain with an example?
- What is a homologous series? Explain with an example.

Advertisements