
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Difference Between Compile-Time and Runtime Polymorphism in Java
If we perform (achieve) method overriding and method overloading using instance methods, it is run time (dynamic) polymorphism.
In dynamic polymorphism the binding between the method call an the method body happens at the time of execution and, this binding is known as dynamic binding or late binding.
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
If we perform (achieve) method overriding and method overloading using static, private, final methods, it is compile time (static) polymorphism.
In static polymorphism the binding between the method call an the method body happens at the time of compilation and, this binding is known as static binding or early binding.
Example
class SuperClass{ public static void sample(){ System.out.println("Method of the super class"); } } public class SubClass extends SuperClas { public static void sample(){ System.out.println("Method of the sub class"); } public static void main(String args[]){ SuperClass.sample(); SubClass.sample(); } }
Output
Method of the super class Method of the sub class
Advertisements