Javatpoint Logo
Javatpoint Logo

Message Passing in Java

In object-oriented programming, communication between objects is a vital aspect of building complex systems. One of the key mechanisms for achieving this communication is message passing. In Java, message passing allows objects to interact with each other by invoking methods and passing data between them. In this article, we will explore the concept of message passing in Java and demonstrate its usage through example programs.

Message passing involves sending messages from one object to another, triggering a specific behavior or action. These messages are typically in the form of method invocations, where one object invokes a method on another object to request an operation or exchange information. This communication model enables objects to collaborate and work together to accomplish tasks. To understand message passing better, let's consider a simple scenario of a bookstore system. We'll have two classes: Book and Bookstore. The Book class represents a book with properties like title, author, and price. The Bookstore class represents a bookstore that sells books. The Bookstore class has a method called sellBook(), which takes a Book object as a parameter and performs the necessary operations to sell the book.

Here's the code for the Book class:

And here's the code for the Bookstore class:

In the above example, the Bookstore class uses message passing to sell a book. The sellBook() method takes a Book object as a parameter, which represents the book to be sold. Inside the method, the book's details are displayed by invoking the displayDetails() method on the Book object. This demonstrates how message passing allows objects to interact by invoking methods on each other.

Now, let's create some instances of the Book and Bookstore classes and see message passing in action:

Output:

Book sold:
Title: Java Programming
Author: John Doe
Price: $29.99

In the above example, we create an instance of the Book class called book1 and set its properties using setter methods. We also create an instance of the Bookstore class called bookstore. By invoking the sellBook() method on the bookstore object and passing book1 as a parameter, we initiate message passing between the two objects. The displayDetails() method of the Book object is invoked inside the sellBook() method, resulting in the book's details being displayed.

Message passing is not limited to simple method invocations. It can also involve passing data between objects. Java supports various ways to achieve this, such as passing arguments to methods, returning values from methods, and using instance variables.

Consider the following modified version of the Bookstore class, where we introduce a method called calculateTotalPrice(), which calculates the total price of a list of books:

In this example, the calculateTotalPrice() method takes an array of Book objects as a parameter. It iterates over the array, retrieves the price of each book using the getPrice() method, and calculates the total price. Finally, it returns the total price as a double value.

Let's update the Main class to demonstrate this new functionality:

Output:

Total Price: $49.98

In the updated example, we create two instances of the Book class called book1 and book2 and set their properties. We also create an instance of the Bookstore class called bookstore. We then create an array called books containing both book1 and book2. By invoking the calculateTotalPrice() method on the bookstore object and passing books as a parameter, we initiate message passing once again. The method retrieves the prices of the books using the getPrice() method and calculates the total price, which is then displayed.

In Summary, message passing is a fundamental concept in Java and object-oriented programming, enabling objects to interact with each other by invoking methods and passing data. By understanding and utilizing message passing effectively, developers can create more modular, reusable, and collaborative code. It forms the backbone of building complex systems and fosters the principles of encapsulation and information hiding. So, the next time you design and implement object interactions in Java, remember the power of message passing and its significance in creating robust and flexible software systems.ajs







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA