File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes instantiation of an extended type with baseclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "instantiation of an extended type with baseclass" Watch "instantiation of an extended type with baseclass" New topic
Author

instantiation of an extended type with baseclass

Mike Nightsky
Ranch Hand

Joined: Aug 18, 2001
Posts: 48
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


Win the opportunity to make money on the Internet<br /><a href="http://sweeps.sitesell.com/minirich.html" target="_blank" rel="nofollow">http://sweeps.sitesell.com/minirich.html</a>
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You could just cast your object to the type you want.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mike Nightsky
Ranch Hand

Joined: Aug 18, 2001
Posts: 48
Thanx Mike
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instantiation of an extended type with baseclass
 
Similar Threads
Abstract Class and Constructors
RMI basics help!!!
Overload method
Interface doubt from K&B
inheritance question