Static binding (also known as early binding) and dynamic binding (also known as late binding) are two important concepts in object-oriented programming, including Java. They refer to the resolution of method calls or method invocations during the program's execution. Let's explore the differences between static and dynamic binding in Java:
Static Binding (Early Binding):
Example of static binding:
class Parent { void print() { System.out.println("Parent's print method"); } } class Child extends Parent { void print() { System.out.println("Child's print method"); } } public class Main { public static void main(String[] args) { Parent obj = new Child(); obj.print(); // Static binding based on reference type } } In this example, the obj.print() call is resolved at compile time based on the reference type (Parent), so it invokes the print method of the Parent class.
Dynamic Binding (Late Binding):
Example of dynamic binding:
class Parent { void print() { System.out.println("Parent's print method"); } } class Child extends Parent { void print() { System.out.println("Child's print method"); } } public class Main { public static void main(String[] args) { Parent obj = new Child(); // Upcasting obj.print(); // Dynamic binding based on actual object type } } In this example, the obj.print() call is resolved at runtime based on the actual object's type (Child), so it invokes the print method of the Child class.
Dynamic binding is a fundamental feature of polymorphism in Java, allowing objects of different subclasses to be treated uniformly when calling overridden methods defined in their common superclass. It is a key concept in achieving flexibility and extensibility in object-oriented programming.
markdown django-cache wolfram-mathematica azure-rm-template gpgpu asp.net-mvc-routing matplotlib-animation project-reference mysql-5.0 xcode8