Open In App

Messages, aggregation and abstract classes in OOPS

Last Updated : 11 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Object-Oriented Programming System (OOPS) is the basic concept of many programming languages. It is a paradigm based on the object which contains methods and data. This concept is used to make programming a class and object model which behaves like a real-world scenario. In this article, we’ll learn about messages, aggregation, composition and abstract classes in the OOPS paradigm.

Message Passing: Message Passing in terms of computers is communication between processes. It is a form of communication used in object-oriented programming as well as parallel programming. Message passing in Java is like sending an object i.e. message from one thread to another thread. It is used when threads do not have shared memory and are unable to share monitors or semaphores or any other shared variables to communicate. The following are the main advantages of the message passing technique:

  1. This model is much easier to implement than the shared memory model.
  2. Implementing this model in order to build parallel hardware is much easier because it is quite tolerant of higher communication latencies.

In OOPs, there are many ways to implement the message passing technique like message passing through constructors, message passing through methods or by passing different values. The following is a simple implementation of the message passing technique by the values:

Java




// 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);
    }
}


Output:

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




// 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);
    }
}


Output:

Roll no: 96
Name: Utkarsh
Street: 10
City: Delhi
State: Delhi
Country: India

Abstract Classes: Abstraction is a technique used in OOPS paradigm which shows only relevant details to the user rather than showing unnecessary information on the screen helping in reduction of the program complexity and efforts to understand. Every OOPS implemented language has a different kind of implementation but has the same concept of hiding irrelevant data. Abstract classes are one such way in java to implement the abstraction. In Java, abstract classes are declared using abstract keyword and can have abstract as well as normal methods but, normal classes cannot have abstract methods declared in it. Abstract classes either have a definition or they are implemented by the extended classes. Let’s take an example to understand why abstraction is implemented. In this example, we will create cars manufactured in a specific year. There can be many cars manufactured in a specific year. But the properties of cars doesn’t change. So, the name of the car here is abstract and the remaining properties are constant. The following is an implementation of the above example:

Java




// 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();
    }
}


Output:

Maserati!


Similar Reads

Difference Between Abstract Class and Abstract Method in Java
Abstract is the modifier applicable only for methods and classes but not for variables. Even though we don't have implementation still we can declare a method with an abstract modifier. That is abstract methods have only declaration but not implementation. Hence, abstract method declaration should compulsory ends with semicolons. Illustration: publ
3 min read
Why Java Interfaces Cannot Have Constructor But Abstract Classes Can Have?
Prerequisite: Interface and Abstract class in Java. A Constructor is a special member function used to initialize the newly created object. It is automatically called when an object of a class is created. Why interfaces can not have the constructor? An Interface is a complete abstraction of class. All data members present in the interface are by de
4 min read
Difference Between Aggregation and Composition in Java
Aggregation and composition describe the type of relationships between objects when communicating with each other, this might be used in low-level design to depict associations between objects. In this article, we are going to discuss the differences between Aggregation and Composition in Java programming language. Aggregation It is a special form
7 min read
Association, Composition and Aggregation in Java
Association is a relation between two separate classes which is established through their Objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many. In Object-Oriented programming, an Object communicates to another object to use functionality and services provided by that object. Composition and Aggregation are the two forms of
9 min read
Data Aggregation in Java using Collections Framework
Let's look at the problem before understanding what data aggregation is. You are given daily consumption of different types of candies by a candy-lover. The input is given as follows. Input Table: [caption width="800"]Data Before Aggregation[/caption] Desired Output Table: [caption width="800"]Data After Aggregation[/caption] Above is the aggregate
6 min read
Understanding OOPs and Abstraction using Real World Scenario
Object-oriented programming: As the name suggests, Object-Oriented Programming or OOPs refers to languages that use objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism etc in programming. In this article, we will discuss how this OOP's concept is implemented in real world
4 min read
30 OOPs Interview Questions and Answers (2024)
Object-Oriented Programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is widely used in many popular languages like Java,
16 min read
Difference between Abstract Class and Concrete Class in Java
Abstract Class: An abstract class is a type of class in Java that is declared by the abstract keyword. An abstract class cannot be instantiated directly, i.e. the object of such class cannot be created directly using the new keyword. An abstract class can be instantiated either by a concrete subclass or by defining all the abstract method along wit
5 min read
Difference between Final and Abstract in Java
In this article, the difference between the abstract class and the final class is discussed. Before getting into the differences, lets first understand what each of this means. Final Class: A class which is declared with the "Final" keyword is known as the final class. The final keyword is used to finalize the implementations of the classes, the me
4 min read
Difference between Abstract Class and Interface in Java
Abstract class and interface are both used to define contracts in object-oriented programming, but there are some key differences between them. Difference between abstract class and interface:- Definition: An abstract class is a class that cannot be instantiated and can contain both abstract and non-abstract methods. An interface, on the other hand
11 min read
Practice Tags :