• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

instantiation of an extended type with baseclass

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Mike
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic