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.
Static Binding or Early Binding
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:
Static binding examples:
Here we have two classes Human and Boy. Both the classes have same method walk() but the
method is static, which means it cannot be overridden so even though I have used the object of
Boy class while creating object obj, the parent class method is called by it. Because the reference
is of Human type (parent class). So whenever a binding of static, private and final methods
happen, type of the class is determined by the compiler at compile time and the binding happens
then and there.
Dynamic Binding or Late Binding
When compiler is not able to resolve the call/binding at compile time, such binding is known as
Dynamic or late binding. Method Overriding is a perfect example of dynamic binding as in
overriding both parent and child classes have same method and 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 so this is known as dynamic binding.
Dynamic binding example:
This is the same example that we have seen above. The only difference here is that in this
example, overriding is actually happening since these methods are not static, private and final. In
case of overriding the call to the overridden method is determined at runtime by the type of
object thus late binding happens.
Task1: Execute following codes and provide output? Also explain which type of binding is
implemented in each code?
Code 1:
public class NewClass
{
public static class superclass
{
static void print()
{
System.out.println("print in superclass.");
}
}
public static class subclass extends superclass
{
static void print()
{
System.out.println("print in subclass.");
}
}
public static void main(String[] args)
{
superclass A = new superclass();
superclass B = new subclass();
A.print();
B.print();
}
}
SOLUTION:
Static binding is implemented in the above code.
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:
public class NewClass
{
public static class superclass
{
void print()
{
System.out.println("print in superclass.");
}
}
public static class subclass extends superclass
{
@Override
void print()
{
System.out.println("print in subclass.");
}
}
public static void main(String[] args)
{
superclass A = new superclass();
superclass B = new subclass();
A.print();
B.print();
}
}
SOLUTION:
Dynamic binding is implemented in the above code.
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/