Messages, aggregation and abstract classes in OOPS

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Maserati!

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.