Software Construction: Military College of Signals
Software Construction: Military College of Signals
SOFTWARE CONSTRUCTION
Lab Submission: 07
Name: Zahra Qamar
Course: BESE 23C
Experiment # 7: Static Binding vs Dynamic Binding
Association of method call to the method body is known as binding. There are two types of
binding: Static Binding that happens at compile time and Dynamic Binding that happens at
runtime.
The binding which can be resolved at compile time by compiler is known as static or early
binding. The binding of static, private and final methods is compile-time. The reason is that these
method cannot be overridden and the type of the class is determined at the compile time. Lets see
an example to understand this:
Code 1:
Explanation:
The method print () is static and hence the static, final and private methods cannot be overridden
so even though the object of subclass class is being used while creating object B, the parent class
method is called by it. Because the reference is of superclass type (parent class). It is determined
at the compile time, the type of the class for which the method is to be executed.
Code 2:
Explanation:
This is because the method print () in both the classes is not static, final or private and hence can
easily be overridden. In this case the type of the object determines which method is to be
executed. The type of object is determined at the run time. The object B is of type subclass, but
the reference is of superclass (parent class) therefore print () method of subclass is executed.
References:
https://www.javatpoint.com/static-binding-and-dynamic-binding
https://www.geeksforgeeks.org/static-vs-dynamic-binding-in-java/