class Operation{
int square(int n){
return n*n;
}
}
class Circle{
Operation op;//aggregation
double pi=3.14;
double area(int radius){
op=new Operation();
int rsquare=op.square(radius);//code reusability (i.e. delegates the method call).
return pi*rsquare;
}
public static void main(String args[]){
Circle c=new Circle();
double result=c.area(5);
System.out.println(result);
}
}
78.5
We’ve created a reference to the Operation class in the Circle class in this example.
import java.io.*;
import java.util.*;
// class College
class College {
public String name;
public String address;
College(String name, String address)
{
this.name = name;
this.address = address;
}
}
// University has more than one college.
class University {
// reference to refer to list of college.
private final List<College> colleges;
University(List<College> colleges)
{
this.colleges = colleges;
}
// Getting total number of colleges
public List<College> getTotalCollegesInUniversity()
{
return colleges;
}
}
class CompositionExample {
public static void main(String[] args)
{
// Creating the Objects of College class.
College c1
= new College("XYZ Engineering College", "Kolkata");
College c2
= new College("ABC Engineering College", "Delhi");
College c3 = new College("Coderz College of Engineering & Management Sudies",
"Bombay");
// Creating list which contains the no. of colleges.
List<College> college = new ArrayList<College>();
college.add(c1);
college.add(c2);
college.add(c3);
University university = new University(college);
List<College> colleges = university.getTotalCollegesInUniversity();
for (College cg : colleges) {
System.out.println("Name : " + cg.name
+ " and "
+ " Address : " + cg.address);
}
}
}
Name : XYZ Engineering College and Address : Kolkata
Name : ABC Engineering College and Address : Delhi
Name : Coderz College of Engineering & Management Sudies and Address : Bombay
We create the College class, which contains variables such as name and address. We also create a class called University, which has a reference to the college list. There can be multiple collages at a university. As a result, if a university closes permanently, all colleges within that university must close as well, because colleges cannot exist without a university. Composition is the relationship between the university and the colleges.
Note: also read about the Inheritance in Java
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…