Home > Java > javaTutorial > Messages, aggregates and abstract classes in Java

Messages, aggregates and abstract classes in Java

WBOY
Release: 2023-08-24 08:29:11
forward
1063 people have browsed it

Messages, aggregates and abstract classes in Java

In contemporary computer programming practice, it is common practice to use object-oriented programming systems (OOPS) as the basis of programming languages. This paradigm combines methods with data to produce beneficial results for developers. Adopting OOPS allows programmers to create an accurate class and object model that enables seamless work by effectively replicating real-life scenarios.

In this article, learn about messages, aggregates, and abstract classes in the OOPS paradigm.

What is a message?

In the computer field, message passing refers to communication between processes. Data transmission is an efficient communication method in parallel programming and object-oriented programming practices. When using Java, sending messages across different threads is closely related to the process of sharing objects or messages. Unlike shared monitors, semaphores, or similar variables, this approach is useful where there are possible barriers to thread interaction without a cooperative storage mechanism; Messaging methods can be performed in object-oriented programming through constructors, methods, or by sending various values.

The main advantages of message forwarding technology are as follows:

  • Compared with the shared memory mode, the implementation of this mode is much simpler.

  • Because this method has a high tolerance for higher connection latency.

  • The process of implementing this as parallel hardware is much simpler.

grammar

public class MessagePassing {
   // body
}
Copy after login

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);
   }
}
Copy after login

Output

Int value is : 101
Float value is : 20.7
Copy after login

What is aggregation?

In a unique sense, this is an association type. Aggregation is a one-way directed relationship that accurately expresses the HAS-A relationship between classes. Furthermore, when two classes are aggregated, terminating one has no effect on the other. It is often designated as a weak relationship compared to the combination. In contrast, the parent class owns the child entities, which means that the child entities cannot be accessed directly and cannot exist without the parent object. In contrast, in an association, both parent and child entities can exist independently.

grammar

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

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);
   }
}
Copy after login

Output

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

What is an abstract class?

Abstraction is a method used in the object-oriented programming paradigm to reduce program complexity and understanding effort by showing the user only relevant information instead of irrelevant information on the screen. Although the implementation differs, the idea of ​​hiding useless data is the same in every language in which an object-oriented programming system is implemented. One technique to achieve abstraction in Java is to use abstract classes. Java allows both abstract and regular methods to be declared in a class, but abstract methods cannot be expressed in regular classes. Abstract classes are either defined or implemented by extension classes.

grammar

abstract class A{}
Copy after login

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();
   }
}
Copy after login

Output

Maserati!
Copy after login

in conclusion

OOPS is a basic concept in many programming languages. It is a paradigm based on objects containing methods and data. Message passing is a form of communication used in object-oriented programming languages ​​and parallel programming. Aggregation is a form of association in a unique sense, and it is a strictly directional association. Abstraction is a technique used in object-oriented programming languages ​​that exposes only relevant details to the user.

The above is the detailed content of Messages, aggregates and abstract classes in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template