- This model is much easier to implement than the shared memory model.
- Implementing this model in order to build parallel hardware is much easier because it is quite tolerant of higher communication latencies.
// Java program to demonstrate
// message passing by value
import java.io.*;
// Implementing a message passing
// class
public class MessagePassing {
// Implementing a method to
// add two integers
void displayInt(int x, int y)
{
int z = x + y;
System.out.println(
"Int Value is : " + z);
}
// Implementing a method to multiply
// two floating point numbers
void displayFloat(float x, float y)
{
float z = x * y;
System.out.println(
"Float Value is : " + z);
}
}
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating a new object
MessagePassing mp
= new MessagePassing();
// Passing the values to compute
// the answer
mp.displayInt(1, 100);
mp.displayFloat((float)3, (float)6.9);
}
}
Int Value is : 101 Float Value is : 20.7
Aggregation: This is a form of association in a special way. Aggregation is strictly directional association (i.e.), one-way association and represents HAS-A relationship between the classes. Also, in aggregation, ending one of the classes between the two does not affect another class. It is often denoted as a weak association whereas composition is denoted as a strong association. In composition, the parent owns the child entity that means child entity will not exist without parent entity and also, child entity cannot be accessed directly. Whereas, in the association, the parent and child entity can exist independently. Let's take an example to understand this concept. Lets take a student class and an Address class. The address class represents the student address. Since every student has an address, it has an has a relationship. However, the address is completely independent of student. If some student leaves the school or the college, he or she still has an address. This means that the address can survive independently without the student. Therefore, this is an aggregation. The following is an implementation of the above example:
// Java program to demonstrate
// an aggregation
// Implementing the address
// class
class Address {
int strNum;
String city;
String state;
String country;
// Constructor of the address
// class
Address(int street, String c,
String st, String count)
{
this.strNum = street;
this.city = c;
this.state = st;
this.country = coun;
}
}
// Creating a student class
class Student {
int rno;
String stName;
// HAS-A relationship with the
// Address class
Address stAddr;
Student(int roll, String name,
Address addr)
{
this.rno = roll;
this.stName = name;
this.stAddr = addr;
}
}
class GFG {
// Driver code
public static void main(String args[])
{
// Creating an address object
Address ad
= new Address(10, "Delhi",
"Delhi", "India");
// Creating a new student
Student st
= new Student(96, "Utkarsh", ad);
// Printing the details of
// the student
System.out.println("Roll no: "
+ st.rno);
System.out.println("Name: "
+ st.stName);
System.out.println("Street: "
+ st.stAddr.strNum);
System.out.println("City: "
+ st.stAddr.city);
System.out.println("State: "
+ st.stAddr.state);
System.out.println("Country: "
+ st.stAddr.country);
}
}
Roll no: 96 Name: Utkarsh Street: 10 City: Delhi State: Delhi Country: India
// Java program to demonstrate the
// abstract class
// Implementing the abstract class
// Car
abstract class Car {
// A normal method which contains
// the details of the car
public void details()
{
System.out.println(
"Manufacturing Year: 123");
}
// The name of the car might change
// from one car to another. So,
// implementing an abstract method
abstract public void name();
}
// A class Maserati which
// extends the Car
public class Maserati extends Car {
// Naming the car
public void name()
{
System.out.print(
"Maserati!");
}
// Driver code
public static void main(String args[])
{
Maserati car = new Maserati();
car.name();
}
}
Maserati!