• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object Reference Conversion  RSS feed

 
lakshmi nair
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rules of Object Reference Conversion (RHE) says ...
a) Interface type can only be converted to another interface type or Object class.if it is an interface it must be the super interface. Now how will we test these rules when its not possible to instantiate an interface?

b) Also arrays may be converted to Object, Cloneable and Serializable interfaces? Why these two interfaces?
c) Object o = new Sub[5]; This compiles but how'll we access the array members?
Thanx
Lakshmi
 
Udayan Naik
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi lakshmi.The RHE states that the rules are for Object REFERENCE conversion.This means that u need only a reference to test those rules , and not a object.You need not create an object by instantiation for testing those rules.Also observe the examples given in RHE regarding those rules.
All classes are sub-classes of Object.Thus an array can be referenced by an Object reference.Arrays also implement the Cloneable interface which means that calling the clone() method on an array is legal.And Arrays also implement the Serializable interface which means that it is possible to store the array object to persistent storage.
Thus array reference can be converted to Object,Cloneable or Serializable references.

------------------
Come on in !! Drinks are on the house in the Big Moose Saloon !!
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iakshmi,
For question a), try the read and execute the following application which illustrate the usage of interface and cast between interfaces and Object.

Hope this clarifies,
Beno�t
[This message has been edited by Beno�t d'Oncieu (edited November 01, 2000).]
 
lakshmi nair
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was a great thing, Beno�t.
I think i got it.
Also Udayan, how'll we know that arrays implement those intefaces. This seem to be stupid but i checked the api before posting the question.I could not find any.
Thanks
lakshmi
 
yogesh sood
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also look at http://www.javaranch.com/ubb/Forum24/HTML/005188.html for detail rules
 
Udayan Naik
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lakshmi.For information regarding an array implementing the Cloneable interface , look up the Object.clone() method.
All arrays also implement the Serializable interface.You can verify both these statements as below.

And please , no question is stupid if it is helping you to clear your doubts and passing the Certification Exam.
------------------
Come on in !! Drinks are on the house in the Big Moose Saloon !!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!