Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instantiation of an extended type with baseclass

 
Mike Nightsky
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope i did not dublicat a question, but i didn't know how formulate may search query to find my anser. By the way i searched but didn't come up with a n answer.

My question.
I have a base class -> A
and i have a class thats extends A -> Ab (its extends it only in methods not in data)

if i get a A and want a Ab is there a simpler way to acompolish this than
crate an constructor in Ab that takes an A and pulls out the values and puts it into the super()-constructor?

Maybe its a intermediate question, but i didn't came up with a solution.
thanx in advance
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could just cast your object to the type you want.
 
Mike Nightsky
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Mike
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you're trying to do. Maybe this?

A one = new A();
Ab two = (Ab)one;

That won't work because one is an instance of A which IS NOT an Ab. If you really need an Ab you might do what you suggested:

Ab two = new Ab(one);

This is a good time to ask yourself if "extends" was the right way to define Ab. An alternative is to make Ab just extend Object, hold a reference to an A instance, and pass any method calls through to that instance. For polymorphism you could make A and Ab implement the same interface for the methods on A.

Does that fit the problem?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic