// 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:");
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());
}
}
}
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());
}
}
}
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.
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.
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.
// 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 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);
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);
}
}
}
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.
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.
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.
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.
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.
Java
public class Demo { public static void main(String[] args) { displaymethod(); } static void display() { System.out.println("It is an example of static method"); } }
Output:
It is an example of a static method.
It is an example of a 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.
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;
}
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;
}
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.
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.