Skip to main content

Has-A relationship

 

  • Has-A relationship is also known as composition/Aggregation
  • There is no specific keyword to implement Has-A relation, but most of the time new keyword.
  • The main Advantage of Has-a Relationship is reusability of the code.
E.g.-:

class car{
 Engine e=new Engine();
}
class Engine{
    //Engine specific functionality
}

Car Has-A engine reference.

Composition-:
                        Without existing container object if there is no chance of existing contained objects, then container and contained object are strongly associated and this strong association is nothing but composition.
E.g-:
            University consist of several departments without existing University there is no chance of existing department, hence university and department are strongly associated and this strong association is nothing but composition.




Aggregation-:
                        Without existing container object if there is chance of existing contained object then container and contained object are weekly associated and this week association is nothing but aggregation.

E.g.-:
        Department consist of various Professor are contained without existing department there may be a chance of existing Professor objects, hence department and Professor objects are weekly associated and this week association is nothing but Aggregation.
   
 


Note-:
  •      In composition object are strongly associated, whereas in aggregation object are weekly associated
  •      In composition, a container object holds directly contained objects. Where are in aggregation, container object holds just references of contained object.
 IS-A vs Has-A relationship-:

If we want to total functionality of a class automatically, then we should go for IS-A relationship.

 
   
            
If we want of part of functionality, then we should go for Has-A relationship.



Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Operators & Assignment part 3

                                                                                       Operators & Assignment 1-:instanceof Operators 2-:Bitwise Operators                                                                        instanceof Operators we can use instanceof operator to check whether the given object is of a particular type are not. Example-: /* list is a collection of objects. List l=new List(); l.add(Customer); l.add(Student); l.add(Test); Object o=l.get(...