Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Bindings and Coupling in Java

Instructor Name: Muhammad Fayyaz Awan


Department of Computer Science, COMSATS University Islamabad, Wah Campus, Pakistan
Contents
2

 Bindings
 Types of Bindings
 Coupling
 Types of Coupling
Binding in Java
3

“Binding" is about associating an identifier to whatever it


identifies, be it a method, a variable, or a type.

All bindings in Java are static ("early") except for bindings


of instance methods, which may be static or dynamic
("late"), depending on method's accessibility.
Static vs Dynamic Binding
4

Static binding (Early binding) is done during compile-time while dynamic binding
(Late binding) is done during run-time.

Private, final and static methods and variables uses static binding and bonded by
compiler while overridden methods are bonded during runtime based upon type of
runtime object.
Understanding Type (Let's understand the type of instance)
5

1) variables have a type


Each variable has a type, it may be primitive and non-primitive.
int data=30;  
2) References have a type
class Dog
{  
 public static void main(String args[])
{ Dog d1;//Here d1 is a type of Dog }  
}  
3) Objects have a type
An object is an instance of particular java class,but it is also an instance of its superclass.
class Animal{}  
 class Dog extends Animal{  
 public static void main(String args[]){  
  Dog d1=new Dog();  
 }  
}  
//Here d1 is an instance of Dog class, but it is also an instance of Animal.
Static Binding
6

When type of the object is determined at compiled time(by the


class Dog compiler), it is known as static binding.
{
private void eat()
{
System.out.println("dog is eating..."); If there is any private, final or
static method in a class, there is
} static binding.
public static void main(String args[])
{
Dog d1=new Dog();
d1.eat();
}
}
Dynamic Binding
7

class Animal{
void eat() {System.out.println("animal is eating...");}
}
class Dog extends Animal
{
void eat() {System.out.println("dog is eating...");}

public static void main(String args[])


{
Animal a=new Dog(); In this example object type cannot be determined by the
a.eat(); compiler, because the instance of Dog is also an instance of
} Animal. So compiler doesn't know its type, only its base type.
}
Differences b/w Static and Dynamic Binding
8
Types of coupling
9

Coupling in Java is further divided into two types, namely:

• Tight coupling
(It is when a group of classes are highly dependent on one another)

• Loose coupling
(When an object gets the object to be used from external sources, we call it
loose coupling)
Tight coupling (Example)
10

class Subject
{ Explanation:  In this program the
    Topic t = new Topic(); Subject class is dependents on Topic
    public void startReading() class. In this program Subject class is
    { tightly coupled with Topic class it
        t.understand(); means if any change in the Topic
    } class requires Subject class to
} change. For example, if Topic class
class Topic understand() method change to
{ gotit() method then you have to
    public void understand() change the startReading() method
    { will call gotit() method instead of
        System.out.println("Tight coupling concept"); calling understand() method.
    }
}
Loose coupling (Example)
11

public interface Topic


{
    void understand();
}
class Topic1 implements Topic { Explanation:  In this program, Topic1
public void understand() and Topic2 objects are loosely
    {
        System.out.println("Got it");
coupled. It means Topic is an
    } interface and we can inject any of
} the implemented classes at run time
class Topic2 implements Topic {
public void understand()
and we can provide service to the
    { end user.
        System.out.println("understand");
    }
}
public class Subject {
public static void main(String[] args)
    {
        Topic t = new Topic1();
        t.understand();
    }
}
Differences b/w tight and loose coupling
12

•Tight coupling is not good at the test-ability. But loose coupling improves the test
ability.

•Tight coupling does not provide the concept of interface. But loose coupling helps us
follow the GOF (gang of four) principle of program to interfaces, not implementations.

•In Tight coupling, it is not easy to swap the codes between two classes. But it’s much
easier to swap other pieces of code/modules/objects/components in loose coupling.

•Tight coupling does not have the changing capability. But loose coupling is highly
changeable.

GOF / Gang of Four Design Patterns in Java. These are design patterns which were defined by four authors – Erich
Gamma, Richard Helm, Ralph Johnson and John Vlissides in their book Design Patterns: Elements of Reusable Object-
Oriented Software.
Differences b/w tight and loose coupling
13
References
14

• Absolute JAVA Fifth Edition by Walter Savitch


• https://www.geeksforgeeks.org/coupling-in-java/
• https://
www.javatpoint.com/static-binding-and-dynamic-binding
15

THANK YOU

You might also like