Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Java Aggregation

    The relationship between two classes known as "aggregation" in Java is best described as a "has-a" and "whole/part" relationship. This association relationship is a more specialised variation. The aggregate class is said to own the other class because it contains a reference to it. Every class that is cited is taken to be a component of the aggregate class.

    for example student object(schoolStudent) contains many informations such as id, name etc. It contains one more object named subjects , which contains its own informations such as name of book and price as given below.






Usage of Aggregation in Java

  • When there is no is-a relationship, aggregation is also the best method for achieving code reuse.
  • Aggregation should be used rather than inheritance if the relationship is-a is not maintained over the lifetime of the objects involved.

1.Example of Aggregation


Java example of Aggregation
 // Java example of Aggregation 
class Subjects {
    String BookName;
  int bookPrice;
  public Subjects(String bookName, int bookPrice) {
	BookName = bookName;
	this.bookPrice = bookPrice;
  }
}
public class SchoolStudent {
  String name;
  int id;
  Subjects subjects ;
  SchoolStudent(String name, int id, Subjects subjects){
    this.name = name;
    this.id = id;
    this.subjects = subjects;
  }
  public static void main(String[] args) {
	 Subjects subjects = new Subjects("Math",321);
	 SchoolStudent schoolStudent=new SchoolStudent("Alok", 12,subjects);
	 System.out.println("Student Name :"+schoolStudent.name);
	 System.out.println("Student ID :"+schoolStudent.id);
	 System.out.println("Book Name :"+schoolStudent.subjects.BookName);
	 System.out.println("Book Price :"+schoolStudent.subjects.bookPrice);
  }	
}




Output:

Student Name :Alok 
Student ID :12 
Book Name :Math 
Book Price :321 

2.Example of Aggregation


Java example of Aggregation
 // Java example of Aggregation 
class Address {  
    String city,state,country;  
	public Address(String city, String state, String country) {  
	    this.city = city;  
	    this.state = state;  
	    this.country = country;  
	}  
} 
public class SchoolStudent {  
	int RollNo;  
	String StudentName;  
	Address Studentaddress;  
  
	public SchoolStudent(int RollNo, String StudentName,Address Studentaddress) {  
	    this.RollNo = RollNo;  
	    this.StudentName = StudentName;  
	    this.Studentaddress=Studentaddress;  
	}  
  
	void display(){  
		System.out.println("RollNo :"+RollNo+" Name : "+StudentName);  
		System.out.println(Studentaddress.city+" "+Studentaddress.state+" "+Studentaddress.country);  
	}  
  
	public static void main(String[] args) {  
		Address Studentaddress=new Address("Patna","Bihar","India");  
		Address Studentaddress2=new Address("Pune","Maharashtra","India");  		  
		SchoolStudent schoolStudent=new SchoolStudent(401395,"Alok",Studentaddress);  
		SchoolStudent schoolStudent2=new SchoolStudent(401398,"Ankita",Studentaddress2);        
		schoolStudent.display();  
		schoolStudent2.display();        
	}  
}




Output:

Student Name :Alok 
Patna Bihar India 
RollID :401398 Name : Ankita 
Pune Maharashtra India 
Java Polymorphism Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Inheritance (IS-A)
Java Hybrid Inheritance
Aggregation(HAS-A)
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.