• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructing subclasses from superclass instance  RSS feed

 
Michael Diamond
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to be able to create a subclass instance from an existing base class in a generic way. The generic part is where I'm having trouble.



The problem is that JavaUtil.setObjectFields() is not part of the same package as the objects so it will not have access to any non-public fields. Of course I'm not going to declare all my instance fields public, but I don't want to have to copy/paste the setObjectFields() code into each one of my objects either.

Suggestions?
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,

I'm not sure why you would need such a functionality but if you put the setObjectFields() method in the Base Class as,



Shouldn't it work? This is because the second argument "always" is THIS object as you define it in the BaseClass's constructor so if you put the method inthe BaseClass it would be automatically inherited to child classes and you can call it by giving any arguments of valid BaseClass Child from any other valid BaseClass's child.

e.g.
if you had two children of BaseClass, Child1 and Child2
you can call,
Child1.setObjectFields(Child2); which will copy all BaseClass defined fields from Child2 to Child1 object.

Hope I understood your question.

Regards,
Maulin
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fields[i].setAccessible(true);
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ilja,

Can you see what I am suggesting would work or not? Its interesting question.

And I would love to know if I got the picture correctly and if we can avoid even creating JavaUtil's method setObjectFields() when we can really use inheritance power.

Regards,
Maulin
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay...finally I had to write the code,



And the output is,
ch1=i=100,j=200
ch21=i=100,j=200
ch22=i=1000,j=2000


Is this correct behavior as expectations?


Regards,
Maulin
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay..okay...don't scream at me. I get the problem

I guess what Ilja suggested would be the only way out given that we have Security Permissions to call setAccessible() on the Field object..

Regards,
Maulin
 
Michael Diamond
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
fields[i].setAccessible(true);


Isn't this equivalent to setting the fields public?


Maulin: I like the idea of putting the function in the base class, but I have several object heirarchies I would like to use this with and I don't like having duplicate code. The project I inherited suffers greatly from this already so I was trying to avoid putting this function into each base class.
 
Michael Diamond
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am surprised that this doesn't seem to be a common pattern. Let me describe the problem I am trying to solve.

I have two objects, PurchaseRequest and PurchaseOrder. These objects share the same base class, OrderObject. When a request is approved, it is turned into a PurchaseOrder. I would like to create a new PurchaseOrder based upon the PurchaseRequest.


The other option I thought about was cloning, but that does not allow me to create a new subclass from the cloned object. So this is essentially a deep copy of the superclass in the constructor of the subclass.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but I have several object heirarchies I would like to use this with and I don't like having duplicate code.

Thats why I said in my latest post that "I get the problem" I understand what you want but as you think, its difficult to do it in clean way w/o using setAccessible() thing

Regards,
Maulin
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm, maybe they shouldn't have a common superclass. Your constructor idea looks fine:

Maybe PurchaseOrder class keeps the existing PurchaseRequest object as a variable.

If you need to work at an abstract level where you can look at either type, you might make the common methods in an interface:

Now you could have methods work on the abstract type:

Any of that sound tempting?
[ October 14, 2004: Message edited by: Stan James ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!