

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- 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 CheckBoxTreeItem in JavaFX explain with an example?
- What is the character count? Explain with an example?
- What is the use of StringBuffer class can anyone explain with an example?
- What is Image Array? Explain with an example in C++
- What is Bubble Sort in Python? Explain with an example?
- Is RowSet Scrollable? Explain with an example?
- What are jagged arrays and explain with an example in Java?
- What is Parameterized Batch Update in JDBC? Explain with an example?
- What is the difference between String, StringBuffer and StringBuilder classes in Java explain briefly?
- What is meant by Splicing an array in JavaScript? Explain with an example
- Explain trial balance with an example
Advertisements