Messages, aggregation and abstract classes in OOPS
Last Updated :
11 Feb, 2022
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:
- This model is much easier to implement than the shared memory model.
- 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
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 GFG {
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
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
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 addr)
{
this .rno = roll;
this .stName = name;
this .stAddr = addr;
}
}
class GFG {
public static void main(String args[])
{
Address ad
= new Address( 10 , "Delhi" ,
"Delhi" , "India" );
Student st
= new Student( 96 , "Utkarsh" , 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: 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
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();
}
}
|
Share your thoughts in the comments
Please Login to comment...