Aggregation in Java

Association:- Association is to show the relationship between two or more class. It shows Has-a-relationship.

We have tow association concept to show the Has-a- relationship

1. Aggregation

2. Composition

Here I will discuss Aggregation

Aggregation: Aggregation represent has a relationship, It show a weak relationship, means one class is weakly depend on another class.

Ex:- Suppose we have a car class and music class, so without music class car class can execute.

class Music{

private String songName;

Music(String songName){

this.songName=songName;

}

public String getsongName() {

return songName;

}

}

class Car{

private String Name;

private Music m;

Car(String Name, Music m){

this.Name=Name;

this.m=m;

}

public String getName() {

return Name;

}

public Music getMusic() {

return m;

}

}

public class Agreegation {

public static void main(String[] args) {

Music m1=new Music("Aashain hai");

Car c1=new Car("RR",m1);

System.out.println(c1.getName()+" music is "+c1.getMusic().getsongName());

}

}

要查看或添加评论,请登录

Nilu Kumari的更多文章

  • Data Structure #01

    Data Structure #01

    Given an integer array nums, return true if any value appears at least twice in the array, and return false if every…

  • Binary Tree:-

    Binary Tree:-

    Binary:- A binary tree is a recursive data structure where each node can have two children at most. A common type of…

  • Comparable & Comparator in Java

    Comparable & Comparator in Java

    Comparable & Comparator both are interfaces and can be used to sort collection elements. Comparable provides a single…

  • Queue Implementation Using LinkedList

    Queue Implementation Using LinkedList

    Queue:- A queue is defined as a linear data structure that is open at both ends and the operations are performed in…

  • Wrapper Classes

    Wrapper Classes

    Wrapper classes provides the mechanism to convert primitive into object and object into primitive. There are two way to…

  • Doubly Linked List

    Doubly Linked List

    Doubly Linked List:- A doubly linked list is a special type of linked list in which each node contains a pointer to the…

  • LinkedList Implementation In detail.

    LinkedList Implementation In detail.

    LinkedList--> LinkedList is a part of the Collection Framework present in java.util package.

  • Composition in Java

    Composition in Java

    Composition:-composition represents a stronger relationship where one class is composed of one or more other classes…

  • JDBC

    JDBC

    /* * JDBC: Java Database Connectivity * In real word we are seeing that some login page or register page * when a user…

  • Stack Implementation Using LinkedList

    Stack Implementation Using LinkedList

    public class Stack { static class Node{ int data; Node next; Node(int data){ this.data=data; this.

社区洞察

其他会员也浏览了