Java Program to add two numbers using aggregation


In this program, You will learn how to add two numbers using aggregation in java.


20 = 10 + 10

40 = 30 + 10

Example: How to add two numbers using aggregation in java.

class Add {
    int x = 10;
    int y = 20;
}

class Main {

    Add obj;

    void display() {
        obj = new Add();
        System.out.println("The sum is :" + (obj.x + obj.y));
    }

    public static void main(String args[]) {
        Main t = new Main();
        t.display();
    }
}

Output:

The sum is :30