| Author |
is-a relationship and autoboxing.
|
shashank pratap
Greenhorn
Joined: Aug 11, 2009
Posts: 12
|
|
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.
|
 |
geeta vemula
Ranch Hand
Joined: Jul 18, 2008
Posts: 208
|
|
|
IS-A relationship is used in autoboxing. int is autobox to Integer but not to Long since int is not long.
|
 |
Larry Chung
Ranch Hand
Joined: Feb 02, 2010
Posts: 245
|
|
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.
|
SCJP 6
|
 |
Nidhi Sar
Ranch Hand
Joined: Oct 19, 2009
Posts: 252
|
|
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.
|
"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
|
 |
geeta vemula
Ranch Hand
Joined: Jul 18, 2008
Posts: 208
|
|
|
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.
|
 |
Astha Sharma
Ranch Hand
Joined: Oct 15, 2011
Posts: 128
|
|
|
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.
|
 |
Seetharaman Venkatasamy
Bartender
Joined: Jan 28, 2008
Posts: 4503
|
|
Larry Chung wrote: local variable and still create a HAS-A relationship.
No. you cant access it outside the method.
|
Not everything that counts can be counted, and not everything that can be counted counts-Albert Einstein
|
 |
Seetharaman Venkatasamy
Bartender
Joined: Jan 28, 2008
Posts: 4503
|
|
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?
|
 |
Helen Ma
Ranch Hand
Joined: Nov 01, 2011
Posts: 319
|
|
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
Joined: Nov 01, 2011
Posts: 319
|
|
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.
|
 |
 |
|
|
subject: is-a relationship and autoboxing.
|
|
|