163 subscribers
3.42K photos
24 videos
39 files
371 links
Link: @java_posts

Contact: java.response.email@gmail.com
Download Telegram
public class College{
public void status() {
System.out.println("College is open today");
}
}
public class Student{
College obj = new College();
public void goToCollege() {
obj.status();
}
}
In the above code example, the student class is dependent on the college class. That is, any change in the college class requires student classes to change. Here, therefore, student class and college class are tightly coupled with each other.

* Loose coupling: If one class is weakly interrelated to another class, it is said to have loose coupling with that class. Loose coupling is preferred over tight coupling. A class can achieve this with the help of interfaces, as shown below.
public interface College {
void status();
}
class CollegeStatus1 implements College{
public void status() {
System.out.println("College is open monday to friday");
}
}
class CollegStatus2 implements College{
public void status() {
System.out.println("College is open in saturday");
}
}
public class Student{
College obj = new CollegeStatus1();
public void goToCollege() {
obj.status();
}
}
In the above code example, CollegeStatus1 and CollegeStatus2 are loosely coupled. Here, student class is not directly or tightly coupled with a CollegeStatus1 or CollegeStatus2 class. By applying a dependency injection mechanism, the loose coupling implementation is achieved to allow a student to go to college with any class which has implemented a college interface. In addition, it means we can use CollegeStatus2 whenever the college is open on Saturday.
Cohesion In Java
Java Cohesion measures how the methods and the attributes of a class are meaningfully and strongly related to each other and how focused they are on performing a single well-defined task for the system. This is used to indicate the degree to which a class has a single, well-focused responsibility. More cohesive classes are good to keep them for code reusability. Low cohesive classes are difficult to maintain as they have a less logical relationship between their methods and properties. It is always better to have highly cohesive classes to keep them well focused for a single work.

* Low Cohesion: In the following code, we have a class called Book. But it is less cohesive because it comprises less focussed and independent attributes and methods to the class. This class should contain information related to the Book. Therefore, the person’s name and age method are making this classless cohesive.
class Book {
int price = 299; // related attribute
String name = "Sam"; // unrelated attribute
// related methods to Book class
public String author(String name) {
return name;
}
public String title(String subject) {
return subject;
}
public int id(int number) {
return number;
}
// unrelated methods to Book class
public int age(int age) {
return age;
}
}
*High Cohesion: When the class has a single well-defined purpose or task, it is said to be highly cohesive. So, in the above example code, if we remove the information related to the person, then the class becomes highly cohesive, as shown below.
class Book {
int price = 299; // related attribute
// related methods to Book class
public String author(String name) {
return name;
}
public String title(String subject) {
return subject;
}
public int id(int number) {
return number;
}
}
Association in Java
Association is a relation between two separate classes that establishes with the help of their Objects. It specifies the relationship between two or more Objects. Association can be one-to-one, one-to-many, many-to-one, and many-to-many. Let us understand this with real-world examples, suppose the relationship between the bus and the passengers. A bus can have only one driver(one-to-one). Many passengers can associate with the single bus(many-to-one). A single passenger can associate with many different buses(one-to-many). Also, many passengers can associate with the many different buses(many-to-many). One object is associated with another object to use the functionality and services provided by another object.
// class bus
class bus
{
private String name;
// bus name
Bus(String name)
{
this.name = name;
}
public String getBusName()
{
return this.name;
}
}
// passenger class
class Passenger
{
// passenger name
private String name;
// passenger seat id number
private int seatId;
Passenger(String name, int searId)
{
this.name = name;
this.seatId = searId;
}
public String getPassengerName()
{
return this.namel
}
public int getPassengerId()
{
return this.seatId;
}
}
// association between both the
// classes in the main method
class Demo
{
public static void main(String[] args)
{
Bus bus = new bus(""Shree Travels");
Passenger psg = new Passenger(""Sneha", 52);
System.out.println(psg.getPassengerName() + " with seat number " + psg,getPassengerId() + " is a passenger of " + bus.getBusName());
}
}
Output:

Sneha with seat number 52 is a passenger of Shree Travels

Explanation:

In the above example, two separate classes Bus and Passenger, are associated through their Objects inside the class Demo. In this way, we can establish the relationship between two different classes by using the concept of association. A bus can have many passengers, So it is a one-to-many relationship.

Association is of two types, they are:
1. Aggregation
2. Composition

Let’s discuss the two in detail.
Aggregation
Java Aggregation is a weak association and represents a relationship between an object containing other objects. This represents a part of a whole relationship where a part can exist without a whole. Let’s take an example of the relationship between Group and Person. A Person may belong to multiple Groups. Hence a Group can have multiple Persons. But if we delete a Group, the Person object will not destroy. Aggregation represents the Has-A relationship, unidirectional association, i.e., a one-way relationship. For instance, the group can have persons, but vice versa is not possible and thus unidirectional. In this section, both entries can survive individually, which means ending one entity will not affect the other entity. Hence, both objects are independent in aggregation.
import java.util.*;

// person class
class Person
{
private String name;
private int age;
Person(String name, int age)
{
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
/*Group class contains the list of person
Objects. It is associated which the person
class through its object(s) */

// group class
class Group
{
private String groupName;
private List<Person> persons;
Group(String groupName, List<Person> persons)
{
this.groupName = groupName;
this.persons = persons;
}
}
// main method
class Demo
{
public static void main(Sting[] args)
{
// creating objects of person class
Person a = new Person("Tanmay", 17);
Person b = new Person("Sam", 18);
Person c = new Person("Tanmay", 19);
Person d= new Person("Khushi", 20);
// making a list of persons belongs to social welfare group
List<Person> p1 = new ArrayList<>();
p1.add(a);
p1.add(c);
// making a list of persons belongs to drama fest group
List<Person> p2 = new ArrayList<>();
p2.add(b);
p2.add(d);
// creating objects of group class
Group swGrp = new Group("Social Welfare", 1);
Group dfGrp = new group("Drama Fest", p2);
// before deleting drama fest group
SYstem.out.println("List of persons in Drama Fest group:");
for(Person p : p2) {
System.out.println("Person name: " + p.getName() + " , Age: " + p.getAge() + " , Group: Drama Fest");
}
// deleting drama fest group
dfGrp = null;
// after deleting drama fest group
// person list will not destroy
System.out.prnitln("Person name: " + p.getName() + " , Age: " + p.getAge());
}
}
}
output
Explanation:
Here, we can see that the two classes Person and Group, are associated with each other with the help of objects. There are two groups social welfare and drama fest. We created these groups by using the person class. The group has a list of persons. We have two people Sam and Khushi, in the Drama Fest group as shown in the output. Afterwards, we deleted this group by setting the instance of group equals to null. But, our list of persons remains undestroyed due to the weak association, i.e., aggregation, even after the group was deleted.
Composition in Java
Java Composition is an association that represents a part of a whole relationship where a part cannot exist without a whole. Let’s take an example of the relationship between School and Room. The school object consists of several rooms. Whenever the school object destroys automatically, all the room objects will be destroyed, i.e., without the existing school object, there is no chance of an existing dependent object. So these are strongly associated, and this relationship is called composition. If a whole is deleted, then all parts are deleted. So composition represents the part-of relationship.

Whenever there is a composition between two entities, the created object cannot exist without the other object. Thus, in composition, both entities are dependent on each other.