Pin It

How Composition (Has-A) relationship works in Java

Program to demonstrate how Composition (Has-A) relationship works in Java

class SimCard {
 
   private String cardNumber;
 
   public SimCard(){
  
 System.out.println("SimCard Object Constructed");
 cardNumber = "New SimCard Constructed";
  
   }
 
   @Override
   public String toString() {
 
 return cardNumber;
 
   } 
}

public class Mobile {
 
   private SimCard sim = new SimCard();
   private String mobile = "Nokia";
   private int model = 1100;
 
   @Override
   public String toString() {
 
 return sim + " for " + mobile + " " + model;
 
   }
 
   public static void main(String[] args) {

 Mobile mob = new Mobile();
 System.out.println(mob);

   }

}


Output of the program : 

Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot