- 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 and overloading under polymorphism in java?
Overriding − If super class 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 super class type the method of the super class is executed.
Or, if the object is of the type subclass then the method of the super class is executed.
Example
class SuperClass{ public static void sample(){ System.out.println("Method of the super class"); } } public class RuntimePolymorphism extends SuperClass { public static void sample(){ System.out.println("Method of the sub class"); } public static void main(String args[]){ SuperClass obj1 = new RuntimePolymorphism(); RuntimePolymorphism obj2 = new RuntimePolymorphism(); obj1.sample(); obj2.sample(); } }
Output
Method of the super class Method of the sub class
Overloading − If a class have two or more methods with the same name and different parameters. JVM calls the respective method based on the parameters passed to it, at the time of method call.
Example
public class OverloadingExample { public void display(){ System.out.println("Display method"); } public void display(int a){ System.out.println("Display method "+a); } public static void main(String args[]){ OverloadingExample obj = new OverloadingExample(); obj.display(); obj.display(20); } }
Output
Display method Display method 20
- Related Articles
- Difference between Method Overloading and Method Overriding in Java
- What is runtime polymorphism or dynamic method overloading?
- Function Overloading and Overriding in PHP
- Difference Between Function Overloading and Overriding in C++
- What is the difference between compile time polymorphism and runtime polymorphism in java?
- What is Overloading in Java?
- What is method overloading in Java?
- What is constructor overloading in Java?
- Polymorphism in Java
- What is polymorphism in C# ?
- What is the difference between method hiding and method overriding in Java?
- Runtime Polymorphism in Java
- What is the difference between method overloading and method hiding in Java?
- What is Dynamic Polymorphism in C#?
- What is overriding in Java can explain briefly with an example?

Advertisements