// 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.
Equals Method in Java
As the name suggests in java, .equals() is a method used to compare two objects for equality. The .equals() method in java is used to check if the two strings have similar values. It checks them character by character. One should not confuse .equals() method with == operator. The String equals() method compares the two given strings based on the content of the string, whereas the == operator is used for address comparison. If all the contents of both the strings are the same, then .equals() returns true otherwise, it returns false. If all characters are not matched, then it returns false.
Let us understand this with the help of an example:
As the name suggests in java, .equals() is a method used to compare two objects for equality. The .equals() method in java is used to check if the two strings have similar values. It checks them character by character. One should not confuse .equals() method with == operator. The String equals() method compares the two given strings based on the content of the string, whereas the == operator is used for address comparison. If all the contents of both the strings are the same, then .equals() returns true otherwise, it returns false. If all characters are not matched, then it returns false.
Let us understand this with the help of an example:
public class Demo {
public static void main(String[] args)
{
String s1 = "GreatLearning";
String s2 = "GreatLearning";
String s3 = new String("GreatLearning");
System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // false
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // true
}
}
public static void main(String[] args)
{
String s1 = "GreatLearning";
String s2 = "GreatLearning";
String s3 = new String("GreatLearning");
System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // false
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // true
}
}
Message Passing in Java
Message Passing in terms of computers is a communication phenomenon between the processes. It is a kind of communication used in object-oriented programming. Message passing in Java is the same as sending an object, i.e., a message from one thread to another thread. It is utilized when threads do not have shared memory and are not able to share monitors or any other shared variables to communicate. In message passing calling program sends a message to a process and relies on that process to run its own functionality or code. Message passing is easy to implement, has faster performance, and we can build massive parallel models by using it.
Message Passing in terms of computers is a communication phenomenon between the processes. It is a kind of communication used in object-oriented programming. Message passing in Java is the same as sending an object, i.e., a message from one thread to another thread. It is utilized when threads do not have shared memory and are not able to share monitors or any other shared variables to communicate. In message passing calling program sends a message to a process and relies on that process to run its own functionality or code. Message passing is easy to implement, has faster performance, and we can build massive parallel models by using it.