• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

is-a relationship and autoboxing.

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in Kathy +Bert CD

which are true?(choose all that apply)
1.has a relationships are , by definition , well encapsulated.
2.a covariant return requires the existence of a has -a relationship.
3.overriding requires the existence of a is-a relationship.
4.in some cases, is=-a relationships are used in the process of autoboxing.
5.has-a relationships are always defined with instance variables.


can any one please clear the concept the option 4. which is also one of the correct answer.
option 5. is not in the correct answer list , which is very confusing.

it says the correct answer is 3 and 4.

Please help
shashank pratap.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IS-A relationship is used in autoboxing. int is autobox to Integer but not to Long since int is not long.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shashank pratap wrote:in Kathy +Bert CD
...
can any one please clear the concept the option 4. which is also one of the correct answer.
option 5. is not in the correct answer list , which is very confusing.

it says the correct answer is 3 and 4.



In Kathy + Bert book, the two-minute drill states that:

HAS-A means an instance of one class "has a" reference to an instance of another class or another instance of the same class.



That reference can be a class variable or a local variable and still create a HAS-A relationship. Someone, please tell if that is wrong thinking.
 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

geeta vemula wrote:IS-A relationship is used in autoboxing. int is autobox to Integer but not to Long since int is not long.



I think IS-A relationship model is used for objects only, not for primitives. int and long are both primitives so the whole IS-A model does not apply to them.
 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the question here is relating IS-A with autoboxing. int is autoboxed to Integer but if you have Integer you cannot box it to Long, since Integer is not Long.
 
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Someone, please tell me why option 1 "has a relationships are , by definition , well encapsulated" is not correct. I think it is also a correct option. Is a reference variable in declared as member of class, it is said to be incapsulated in the class.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Larry Chung wrote: local variable and still create a HAS-A relationship.


No. you cant access it outside the method.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Astha Sharma wrote:tell me why option 1 "has a relationships are , by definition , well encapsulated" is not correct.


is below idiom is well encapsulated?
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

case 1 :
static void methodA (Object a) { ... }
methodA (1) is invoked.
the int 1 is auto boxed into Integer. Integer is a Object, so it works.

case 2:
static void methodA(Number a) { ...}
methodA(1) is invoked.

the int 1 is auto boxed into Integer. Integer is a Number, so it works.

case 3:
static void methodA(Long l){ ...}
the int 1 is auto boxed into Integer. Integer is not a Long, so it won't compile.

I hope it helps.

 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
case 4
static void methodA(Number ... a) {...}
methodA(1) is invoked.

The int 1 is auto boxed into Integer. The method takes an array of Number objects. The Integer object is the a[0] in the argument.
So, it works.
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic