Messages, Aggregation and Abstract classes in OOPS

By | August 6, 2020

1. Message


Message in OOPS refers to data used during interaction of different objects. The objects interact with each other by passing messages(data). To interact with each other objects do not need to know anything about each other. They just need to know the type of data(message) accepted and the type of data(message) returned by the other object.


2. Aggregation (Has-a relationship)


If a class has an entity reference, it is known as Aggregation.
Or to simply state it, if a class contains objects of different class types as it’s instance variables, it is aggregation.

Example:-
Let us consider a record of a student with all his information like his student id, name, phone number, address etc.

Let us break name and address into several other parts like name in first name, middle name, last name and address into street, pin code, city, state.
One way of representing all the above information is by creating a class which have all the above details in a single class without using any aggregation at all.

class StudentRecord {
//unique id provided by school
int studentid;

//strings for holding all the name details 
// i.e., : first name, middle name, last name
String firstName,middleName,lastName;

//long integer for holding phone number, 
//since it is 10 digits long
long phoneNumber;

//various data instances for holding address
int pincode;
String street,city,state;
}

Say now we have to create a class to hold faculty records. Now we need to create a class for storing details of faculty members of a school and we will need to rewrite the code for storing address and name data again.
Also if school chooses to change the format to store address or name then we need to make changes in each of these classes individually.

Let us rewrite the above code by Aggregation and see it’s advantages.

//A class for all Name attributes.
class Name {
String firstName, middleName, lastName;
}

//A class for all Address attributes.
class Address {
int pincode;
String street, city, state;
}
class StudentRecords {

//Aggregation
Name name;
long phoneNumber;

//Aggregation
Address address;
}

Now if school either wants to add faculty records we can create another class named FacultyRecords and use Address and Name class directly without having to rewrite code for it. Also, if now the school changes Name format to firstName and lastName only removing middleName we only need to make change in Name class while we needed to make changes to all classes where name was defined when we were not using aggregation.

Advantages:

  • Code Reusability
  • Code Optimisation
  • Easy to introduce changes

Note –
It is preferred to use Aggregation only when there is no inheritance(is-a relationship) present in the code.

3. Abstraction


It is a method of hiding implementation from the user.

Abstract Classes :

  • Abstract classes can be defined as the one which either contains an implemented method or just a method definition.
  • Abstract classes provide abstraction(0% – 100%).
  • Any class with an abstract keyword is an abstract class.
  • Abstract class’ object cannot be created. These classes have to be inherited to be used for any practical implementation.
  • Abstract class’ methods may or may not be overridden.

Syntax:

abstract class <class-name> {
    //method declarations or method definitions
}

Example:

abstract class Car{
    boolean full;
    public void horn(){}//method with no implementation
    //method with implementation
    public void fuelFilled(boolean full){
        full=true;
    }
}