Coupling in Java
In object-oriented design, Coupling refers to the degree of direct knowledge that one element has of
another. In other words, how often do changes in class A force-related changes in class B.
Types of Coupling
There are two types of coupling:
1. Tight coupling:
In general, Tight coupling means the two classes often change together. In other words, if A knows
more than it should about the way in which B was implemented, then A and B are tightly coupled.
Example: If you want to change the skin, you would also have to change the design of your body as
well because the two are joined together – they are tightly coupled. The best example of tight
coupling is RMI(Remote Method Invocation).
JAVA
// Java program to illustrate
// tight coupling concept
class Subject {
Topic t = new Topic();
public void startReading()
{
[Link]();
}
}
class Topic {
public void understand()
{
[Link]("Tight coupling concept");
}
}
Explanation of the above Program: In the above program the Subject class is dependents on Topic
class. In the above program Subject class is tightly coupled with Topic class it means if any change in
the Topic class requires Subject class to change. For example, if Topic class understand() method
change to gotit() method then you have to change the startReading() method will call gotit() method
instead of calling understand() method.
2. Loose coupling
In simple words, loose coupling means they are mostly independent. If the only knowledge that
class A has about class B, is what class B has exposed through its interface, then class A and class
B are said to be loosely coupled. In order to over come from the problems of tight coupling between
objects, spring framework uses dependency injection mechanism with the help of POJO/POJI
model and through dependency injection its possible to achieve loose coupling. Example : If you
change your shirt, then you are not forced to change your body – when you can do that, then you
have loose coupling. When you can’t do that, then you have tight coupling. The examples of Loose
coupling are Interface, JMS.
JAVA
// Java program to illustrate
// loose coupling concept
public interface Topic
{
void understand();
}
class Topic1 implements Topic {
public void understand()
{
[Link]("Got it");
}
} class Topic2 implements Topic {
public void understand()
{
[Link]("understand");
}
} public class Subject {
public static void main(String[] args)
{
Topic t = new Topic1();
[Link]();
}
}
Explanation : In the above example, Topic1 and Topic2 objects are loosely coupled. It means
Topic is an interface and we can inject any of the implemented classes at run time and we can
provide service to the end user.
POJO
POJO in Java stands for Plain Old Java Object. It is an ordinary object, which is not bound by any
special restriction. The POJO file does not require any special classpath. It increases the readability &
re-usability of a Java program.
POJOs are now widely accepted due to their easy maintenance. They are easy to read and write. A
POJO class does not have any naming convention for properties and methods. It is not tied to
any Java Framework; any Java Program can use it.
The term POJO was introduced by Martin Fowler (An American software developer) in 2000. it is
available in Java from the EJB 3.0 by sun microsystem.
Generally, a POJO class contains variables and their Getters and Setters.
The POJO classes are similar to Beans as both are used to define the objects to increase the readability
and re-usability. The only difference between them that Bean Files have some restrictions but, the
POJO files do not have any special restrictions.
Example:
POJO class is used to define the object entities. For example, we can create an Employee POJO class
to define its objects.
Below is an example of Java POJO class:
[Link]:
// POJO class Exmaple
package [Link];
public class Employee {
private String name;
private String id;
private double sal;
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public String getId() {
return id;
}
public void setId(String id) {
[Link] = id;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
[Link] = sal;
}
}
Properties of POJO class
Below are some properties of the POJO class:
o The POJO class must be public.
o It must have a public default constructor.
o It may have the arguments constructor.
o All objects must have some public Getters and Setters to access the object values by other
Java Programs.
o The object in the POJO Class can have any access modifies such as private, public, protected.
But, all instance variables should be private for improved security of the project.
o A POJO class should not extend predefined classes.
o It should not implement prespecified interfaces.
o It should not have any prespecified annotation.
How to use POJO class in a Java Program
The POJO class is created to use the objects in other Java Programs. The major advantage of the
POJO class is that we will not have to create objects every time in other Java programs. Simply we
can access the objects by using the get() and set() methods.
To access the objects from the POJO class, follow the below steps:
o Create a POJO class objects
o Set the values using the set() method
o Get the values using the get() method
For example, create a [Link] class file within the same package and write the following code
in it:
[Link]:
//Using POJO class objects in MainClass Java program
package [Link];
public class MainClass {
public static void main(String[] args) {
// Create an Employee class object
Employee obj= new Employee();
[Link]("Alisha"); // Setting the values using the set() method
[Link]("A001");
[Link](200000);
[Link]("Name: "+ [Link]()); //Getting the values using the get() method
[Link]("Id: " + [Link]());
[Link]("Salary: " +[Link]());
}
}
Output:
Name: Alisha
Id: A001
Salary: 200000.0
From the above example, we can see we have accessed the POJO class properties in [Link].
POJO Vs. Bean
POJO Bean
In Pojo, there are no special restrictions It is a special type of POJO files, which have some special
other than Java conventions. restrictions other than Java conventions.
It provides less control over the fields as It provides complete protection on fields.
compared to Bean.
The POJO file can implement the The Bean class should implement the Serializable interface.
Serializable interface; but, it is not
mandatory.
The POJO class can be accessed by using The Bean class can only be accessed by using the getters and
their names. setters.
Fields may have any of the access Fields can only have private access.
modifiers such as public, private,
protected.
In POJO, it is not necessary to have a no- It must have a no-arg constructor.
arg constructor; it may or may not have
it.
There is not any disadvantage to using The disadvantage of using the Bean is that the Default
the POJO constructor and public setter can change the object state when it
should be immutable.
A Spring Bean is a Java object that is managed by the Spring Framework. In the Spring Framework,
a bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.
The Spring Framework provides a range of features for dependency injection, transaction
management, and other services that can be applied to a bean.
Here is an example of a Spring Bean in Java:
@Component
public class Person {
private String name;
private int age;
@Autowired
private Configuration config;
public Person() {
}
public Person(String name, int age) {
[Link] = name;
[Link] = age;
}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
[Link] = age;
}
}
This Person class is a Spring Bean because it is annotated with the @Component annotation, which
tells the Spring Framework to manage it as a bean. It also has a dependency on
a Configuration object, which is annotated with the @Autowired annotation, indicating that it
should be injected by the Spring Framework.
Spring Beans are managed by the Spring Framework and can be injected into other objects using
dependency injection. They can be easily accessed and manipulated using the Spring Framework
APIs, and can benefit from the various features and services offered by the Spring Framework, such
as dependency injection, transaction management, and others.
So, in summary, a POJO is a basic Java object, a Java Bean is a POJO that follows certain
conventions, and a Spring Bean is a Java object that is managed by the Spring Framework.