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

Contact: java.response.email@gmail.com
Download Telegram
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.
import java.util.*;
// activity room class
class ActivityRoom {
public String subject;
public int id;

ActivityRoom(String subject, int id)
{
this.subject = subject;
this.id = id;
}
}
// department class
class Department {
private String name;
// list of activity rooms in a department
private List<ActivityRoom> ar;

Departmnet(List<ActivityRoom> ar)
{
this.ar = ar;
}
// getting total number of colleges
public List<ActivityRoom>getActivityRoomsInDepartment()
{
return ar;
}
}
class Demo {
public static void main(String[] args)
{
// creating the objects of activity room class
ActivityRoom a1= new ActivityRoom("Technical", 601);
ActivityRoom a2 = new AcivityRoom("Business", 602);
ActivityRoom a3 = new ActivityRoom("Economics", 603);

// making the list of activity rooms
List<ActivityRoom> act = new ArrayList<ActivityRoom>();
act.add(a1);
act.add(a2);
act.add(a3);
// Creating the object of department class
Department d = new Department(act);

// making the list of activity rooms in department
List<ActivityRoom> arlist = d.getActivityRoomsInDepartment();
for (ActivityRoom a : arlist) {
System.out.println(a.subject + " activity room with id " + a.id);
}
}
}
Output

Explanation:

Here we have two classes Activity room and Department. A department composed of different subject activity rooms. So, If the department gets destroyed, then All activity rooms within that department will be destroyed, i.e., the activity room can not exist without the department. That’s why it is composition.
Methods in Java
Java method is a block of code or collection of statements grouped together to complete a certain job or operation. This is used to achieve the reusability of code and can be utilized many times. It also gives easy modification and readability of code. A method is executed only when we call or invoke it. We have two categories of methods in java, i.e., pre-defined and user-defined. Predefined methods are the methods that are already defined in the Java class libraries. When a particular method is written by the user or programmer, it is known as a user-defined method. User-defined methods can be modified according to the requirement.
Let’s discuss:

Static method in Java
The abstract method in Java
Finalize method in Java
Equals method in Java
Static Method in Java
A method that has the static keyword in the declaration is known as the static method. In other words, a method that belongs to a class rather than an instance of a class is known as a static method. We can also create a static method by using the keyword static before the method name. The main benefit of a static method is that we can invoke the static method without even creating an object. It can access static data members and also change their values and is also used to create an instance method. The main() method is a common example of the static method.
public class Demo
{
public static void main(String[] args)
{
displaymethod();
}
static void display()
{
System.out.println("It is an example of static method");
}
}
Abstract Method in Java
A method that is declared with keyword abstract is called an abstract method. The abstract method does not have an implementation or body, or block of code. The abstract method must always be declared in an abstract class, or we can say that if a class has an abstract method, it should be declared abstract. If a class has an abstract method, it should be declared abstract, but vice versa is not true, which means that an abstract class doesn’t need to have an abstract method compulsory. Also, If a normal class extends an abstract class, then the class must have to implement all the abstract parent class’s abstract methods, or it has to be declared abstract.
// abstract class area
abstract class Area {
/* These two are abstract methods. the child class
* must implement these methods
*/
public abstract int areaSquare(int s);
public abstract int areaRectangle(int l, int b);
// normal method
public void display() {
System.out.println("Normal method in abstract class Area");
}
}
// normal class extends the abstract class
class Demo extends Area{

/* If we don't provide the implementation of these two methods, the
* program will throw compilation error
* /
public int areaSquare(int s) {
return s*s;
}
public int areaRectangle(int l, int b) {
return l*b;
}
public static void main(String args[]) {
Area a = new Demo();
System.out.println("Area of square " + a.areaSquare(9));
System.out.println("Area of rectangle " + a.areaRectangle(3,4));
a.display();
}
}
output
Final Method in Java
A method that is declared final is called a final method. We cannot override a final method. This means the child class can still call the final method of the parent class without any problem, but it cannot override it. This is because the main purpose of making a method final is to stop the modification of the method by the sub-class.
class DemoParent {
final void method() {
System.out.println("Parent class final method");
}
}

class Demo extends DemoParent {
// error
void method() {
System.out.println("final method modified inside child class");
}

public static void main(String args[]) {
Demo d = new Demo();
d.method();
}
}
The above code will throw an error as we are trying to modify the final method inside the child class(demo) of the parent class(demoParent).
class DemoParent {
final void method() {
System.out.println("Parent class final method");
}
}

class Demo extends DemoParent {
public static void main(String args[]) {
Demo d = new Demo();
d.method();
}
}
output