Messages, Aggregation and Abstract Classes in Java


In contemporary computer programming practices. It is typical for programming languages to incorporate Object Oriented Programming System (OOPS) as their fundamental basis. This paradigm blends methods with data resulting in beneficial outcomes for developers. Embracing OOPS enables programmers to create an accurate class and object model that works seamlessly by replicating real life scenarios effectively.

Learn about messages, aggregation, and abstract classes in the OOPS paradigm in this article.

What are Messages?

In terms of computers, message passing refers to communication between processes. The transfer of data is an efficient means of communication in both parallel and object-oriented programming practices. When using Java specifically, sending a message across different threads closely follows the process of sharing objects or messages. Unlike shared monitors, semaphores or similar variables where possible hindrances exist for thread interaction without cooperating storage mechanism; this method proves useful. The message passing approach may be executed in OOPs in several ways, including through constructors, methods, or by sending various values.

The message forwarding technique’s key benefits are as follows −

  • Compared to the shared memory paradigm, this one is significantly simpler to implement.

  • Because this approach is highly tolerant of greater connection latencies.

  • Implementing it to create parallel hardware is significantly simpler.

Syntax

public class MessagePassing {
   // body
}

Example

// Java program to demonstrate message passing by value

import java.io.*;
public class MessagePassing {
   void displayInt(int x, int y) {
      int z = x + y;
      System.out.println("Int Value is : " + z);
   }

   void displayFloat(float x, float y) {
      float z = x * y;
      System.out.println("Float Value is : " + z);
   }
}
class Variable {

   public static void main(String[] args) {
      MessagePassing mp= new MessagePassing();
      mp.displayInt(1, 100);
      mp.displayFloat((float)3, (float)6.9);
   }
}

Output

Int value is : 101
Float value is : 20.7

What is Aggregation?

In a unique sense, this is a type of association. Aggregation is a one way directed relationship that precisely expresses HAS-A relationship between classes. Additionally, when two classes are aggregated, terminating one of them has no effect on the other. When compared to composition, it is frequently designated as a weak relationship. In comparison, the parent owns the child entity, which means the child entity cannot be accessed directly and cannot exist without the parent object. Contrarily, in an association, the parent and child entities may both exist in their own right.

Syntax

class Employee {
   int id;
   String name;
   Address address;   // Aggregation
   // body
}

Example

// Java program to demonstrate an aggregation

public class Address {
   int strNum;
   String city;
   String state;
   String country;

   Address(int street, String c, String st, String count) {
      this.strNum = street;
      this.city = c;
      this.state = st;
      this.country = coun;
   }
}
class Student {
   int rno;
   String stName;

   Address stAddr;
   Student(int roll, String name,
      Address address)
   {
      this.rno = roll;
      this.stName = name;
      this.stAddr = address;
   }
}

class Variable {
   public static void main(String args[]) {

      Address ad= new Address(10, "Bareilly", "UP", "India");
      Student st= new Student(1, "Aashi", ad);
      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);
   }
}

Output

Roll no: 1
Name: Aashi
Street: 10
City: Bareilly
State: UP
Country: India

What is Abstract Class?

Abstraction is a method employed in the OOPS paradigm that reduces program complexity and understanding effort by displaying only pertinent information to the user rather than extraneous information on the screen. The idea of concealing useless data is the same in every object-oriented programming system implemented language, despite variations in implementation. One such technique to achieve abstraction in Java is using abstract classes. Java allows for the declaration of both abstract and regular methods in classes, however abstract methods cannot be expressed in regular classes. Either abstract classes have a definition, or the extended classes implement them.

Syntax

abstract class A{}

Example

// Java program to demonstrate the abstract class

abstract class Car {

   public void details() {
      System.out.println("Manufacturing Year: 123");
   }

   abstract public void name();
}
public class Maserati extends Car {
   public void name() {
      System.out.print("Maserati!");
   }
   public static void main(String args[]){
      Maserati car = new Maserati();
      car.name();
   }
}

Output

Maserati!

Conclusion

OOPS is the basic concept of many programming languages. It is a paradigm based on the object which contains methods and data. Message passing is a form of communication used in object-oriented programming languages as well as parallel programming. Aggregation is a form of association in a unique sense and is strictly directional association. Abstraction is a technique used in object-oriented programming language which shows only relevant details to the user.

Updated on: 01-Aug-2023

76 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements