This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Object state  RSS feed

 
Rajan Venkat
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two classes ..let us consider class A and Class B.
I have created an instance of Class A that is OBJ1 and inside class A have declared few instance variables and methods.
The Object state has been changed with lot of method calls to change the instance variables. At this point i am creating an instance of Class B OBJ2 .

With a minimum number of steps have to have the OBJ2 state should be the same as OBJ1 state? How would i acheaive this?

Please let me know ...thanks
 
aslam parveez
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you want to have obj2 to be of the same state as that of obj1 then do you mean that obj2 as well is an instance of Class A. If so why is it that you are having Class B ?
 
Rajan Venkat
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No , OBJ2 is the instance of Class B, there is no connection between OBJ2 and Class A. But i want to acheive the OBJ1 state in OBJ2 with minimum no of steps .

pleaze let me know your thoughts,
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't get it.

OBJ1 is an instance of A
OBJ2 is an instance of B

there is no connection between OBJ2 and Class A.


Then how can a B have the same state as an A unless there is some relationship between the classes. Anyway, the easiest way for an A to know the state of B is to give it the B as a parameter.

 
gaurav abbi
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can do a cloning of obj1 but it will return you Object type and from there you can only take back object of ClassA type by downcasting it to type ClassA.
doncasting to ClassB will through a ClassCasteException at runtime.
i dont see any way,
if you find please let me know also
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!