Why isn't there a simpler way to initialize a subclass with its super class

Let me explain my doubt with an example...
public class Parent {
�    public String parent;
public class Child extends Parent{
�    public String child;
I've an instance of Parent p. I want to construct Child c, with the data in p.
The only way that is provided by Java language seems to be, having a constructor in Child like
public class Child extends Parent{
�    public String child;
�    public Child(Parent p){
�    �    parent = p.parent;
�    }
The problem with this is there is lot of redundant assignment code.
What I don't understand is why there is not a simpler way of doing this when a subclass is after all super class data + some other data(excuse me for not looking at the bahavior part of it)
I'm looking for something as simple as Child c = p, which I know is wrong and know the reasons too.
Yes, we can wrap Child over Parent to do this, but it necessitates repeating all the methods in Parent.
Why is the language writers didn't provide a simple way of doing this? I should be missing something here...I'm just searching for an explanation. May be I'm asking a dumb question, but this bugs me a lot...
Regards,
Kothapalli.

To answer DrClap, I'm demanding it now :-). Let me
not suggest something that Mr.Gosling didn't think of;
he should be having some reasons in not providing it.Because it's essentially impossible in the general case. One of the reasons you may be extending a class is to add new invariants to the superclass.
eg- extend java.awt.Rectangle by BoundedRectangle - extra invariant that none of its corner points may be less than 0 or more than 100 in any dimension. (The rectangle must be entirely contained in the area (0,0)-(100,100))
What would happen if you try to create a BoundedRectangle from a rectangle representing (50,50)-(150,150)? Complete invariant breakdown, with no generic way to handle it, or even recognise it.
Actually, BIJ and sgabie are asking for it. Provide an
automatic copy constructor. Every object should have
an implicit copy constructor defined. From any
subclass, I should be able to invoke
super(parentClass). From the subclass, we can invoke
super(parentClass) first and then go on to initialize the
subclass data.
I really don't know the implementation issues of this,
but what I'm looking for is something like this. I'm just
curious to know the implementation issues.Implementation issue #1: Classes that should not, under any circumstane, be copied.
* eg 1- Singleton objects. If there's an automatic copy constructor, then multiple singletons can be created, making them essentially useless. This by extension kills the Typesafe Enum pattern also.
* eg 2- Objects with extra information, such as java.sql.Connection - if you copied this, would the copy be using the same socket connection, or would another connection be required? What happens if opening another connection, and the JDBC driver requires the password to be entered for every new connection? If the wrong password is entered, what happens to the newly creating connection?
Implementation issue #2: Copying implementation?
* Already mentioned by RPWithey. The only guaranteed way to perform the copy would be with a shallow copy, which may end up breaking things.
Why can't you write the copy constructor yourself? If it's a special case, it only has to be done once. If it's a recurring case, you could write a code generation tool to write them for you, along with most of the rest of the class.

Similar Messages

  • Import a playlist from Spotify to buy the songs on iTunes?  Why isn't there an easy way to do this?  Low hanging fruit Apple.

    Import a playlist from Spotify to buy the songs on iTunes?  Why isn't there an easy way to do this?  Low hanging fruit Apple.

    Yeah, once people get hooked on streaming music, they tend to be much less interested in purchasing downloads, and especially not from a music store that sells songs in an inconvenient (AAC) format instead of the de facto standard, MP3 format.
    However, let me not deter you.  If you would like to make the suggestion to Apple, you can use the iTunes Feedback page:
    http://www.apple.com/feedback/itunesapp.html

  • Why isn't there an easy way to block callers?

    This is such a perpetual nuisance. And it would seem that people as capable as Apple, on a device as powerful as an iPhone, could have figured out an easy solution to this problem. Why isn't "block call" one of the choices on the screen when I get a phone call?

    You may want to look at the Do Not Disturb feature's settings, also. What you could do is add the phone number(s) that can call you to your Favorites list or a group, then set DND to allow those calls to always come through. It will silence everything else.
    ~Lyssa

  • Why isn't there a simple "one click" way (or even an automatic way) to Back Up my thunderbird profile and emails to an external drive so it's always protected??

    I've been using Thunderbird and before that, Netscape, since the dawn of time and it baffles me as to why no one has simplified the process of backing up and/or migrating your profile and emails.
    Wouldn't it be so much easier to simply create a "one click" backup option that copies all the files necessary to restore or transfer your profile, and all my emails, to a new computer or newly reformatted hard drive? There should be a "backup/restore" tab in the options menu for this which would also allow you to set and customize automatic backups, as well as import and restore from and external drive.
    The method you have of digging into the file tree and manually copying the profile folders is cumbersome and confusing at best. Just wondering if anyone has looked into this.
    Thanks

    If you look at the first picture of my reply, note that Export is under Tools/ImportExportTools.
    The add-on is one of many provided by the developer, and is probably one of the most widely used. It is frequently updated, perhaps more often that if it were part of the TB code.
    Another option is to select Help/Troubleshooting Information, click Show Folder, exit TB, go up one level to show the Profiles folder, right-click a profile folder, Add to archive..., then right-click the archive file, Send To... and select your external drive (or just copy the profile folder without adding to an archive).
    If you go up one more level, backing up the Thunderbird folder, you have a complete data set that, with a few minor changes, can be transferred to other computers, even ones with different operating systems

  • Why isn't there an easy way to add a photo to photoStream from the finder?

    Apple makes it easy to share your photo with Twitter, FB, Flickr, etc. Why not Photo Stream? As far as I know, the only way to add a photo to photo stream is to open iPhoto and then add the photo to that.  This seems to be a very poor way of quickly adding a photo.

    If you have a suggestion make it here: Apple Feedback. This is the wrong venue.

  • In cards, why isn't there a simple folded picture card anymore?

    There used to be a simple folded picture card in iphoto cards. You could add writing inside, with no cutesy crap or polka dots, etc. and print it out yourself. Am I missing something?

    All you need to do is find a folded card without the fancy stuff and select the layout you'd like. It's not hard.  For example here's the Holiday Colors theme with a single photo on the front and a plain interior. 
    One just  needs to know how to use iPhoto or experiment with layout options, etc.
    OT

  • Is there a Simple Way to Reformat  Database Text with Custom Converter?

    When a database column contains a string, often the string may be stored in the database in a form that may not be suitable for viewing by the user. It is often interesting to show the user the string after it has been reformatted. Many things including phone numbers, product codes and various other application specific encodings fit into this category. The JSF Converter concept seems to be an ideal place to apply the reformatting via the application of a custom converter. In fact, many of the text books written on JSF refer to the JSF ability to support text reformatting using a custom converter. However, we have not been able to make it work in JSC2_1.
    For example, when a CachedRowSet Table Data Provider is used in a JSC2 application to show tabular data from a database, text columns from the database are automatically bound to StaticText components. Unfortunately, if the developer creates a custom converter and attaches it to the StaticText component, the converter is never called when the object to be converted is already of type String. If the object to be converted is a Date, CLOB or any other type the custom converter will be called - but not for type String. Thus the text is presented to the user directly as stored in the database witout the desired reformatting. The OutputText component on the other hand does appear to support custom converters but there is no possibility within the JSC2 IDE to substitute the OutputText component for the StaticText component in the CachedRowSet Table Data Provider.
    This situation appears to be inconsistent with the JSF specification. Does anyone know if this is a bug or a deliberate design decision or a misuse of the IDE on our part? Last question; has anyone come across a workaround?

    Try using "Examine Document" to remove the OCR output.
    Be well...

  • I just installed FF, and I want a SIMPLE way to copy all my bookmarks. Why isn't there a menu item to do this? The need seems OBVIOUS!

    I simply want to make a copy of all of my wife's "Favorites", i.e. your Bookmarks. In FireFox, PrntScrn won't do it, Windows "Snippets" won't do it, and any HELP answers I've seen are hidiously complex. The need/desire to have a paper copy or Word copy of her bookmarks (of course, WITH URL's) seems completely, blatantly obvious. Why all the secrecy? Why isn't there a MENU item to make a copy of the list of Bookmarks??
    And no, I'm not going to try AGAIN, AGAIN, AGAIN to do the Export/HTLM solution, whatever that means. Sheesh. I'm 10 minutes into FireFox and I'm ready to go back to I.E.

    So you are trying to import bookmarks from IE?
    * [[Import Bookmarks from Internet Explorer]]
    Or export to IE?
    * [[Export bookmarks to Internet Explorer]]

  • Why isn't there a place to select files in the media quiery box in DWCS5.5?

    Why isn't there a place to select files in the media quiery box in DWCS5.5?

    Such a simple solution! I think I gave up on that idea before but I was dragging it to the icon for ME and not the actual queue window. Also looking for a better way to send a clip to the top of the queue without dragging it since I've got so many in line.
    Do you find the quality of the renders the same when using AE?
    I'm outputting to the Youtube 1080p 2997 preset- actually need to figure out the suble differences in all the presets in ME. AE has less options but they are all customizable. When I'd tweak a preset in ME, a lot of times it comes out with a glitch or does not open at all- rendering with the audio button un checked for example, etc.
    Thanks!
    M

  • Why isn't there video clipping with iOS 5.1 on a first gen iPad?

    I have a first gen iPad and can find no way to clip (simple) a video. iMovie can't run, okay. So why isn't there even the usual, simple edit tool in the photo interface? It's ridiculous ... If the ultimate answer is either jailbreak or upgrade, then why buy Apple going forward?

    rawstock wrote:
    I have a first gen iPad ....... why buy Apple going forward?
    I also have a 1st gen and, I imagine unlike you, I just can't recall Apple as ever having advertised or promoted it as the perfect platform for video editing .. or, for that matter, ever having addressed that issue at all. I can't think of any apps that would have been able to do that sort of thing before the advent of the iPad2, either; why would there have been, seeing as how the 1 lacked a camera?
    I have no idea if any of the video editing apps now in the Store will work on the '1' model, but wouldn't be too surprised if they did not. There has to be a miniscule market for that.
    Ah well. You've made up your mind about not buying Apple in the future, right? I really have no interest at all in trying to change your mind.

  • Ok...so I have the latest version of iTunes...why isn't there a cloud icon next to the movie I just bought?  I assume I need it to download from iCloud.

    Ok...so I have the latest version of iTunes per response to my previous question.  Why isn't there a cloud icon next to the movie I just downloaded?  I assume that's the only way to access movies on the cloud.  ( My computer shows the icon.)

    What are you trying to do? If the movie is on your iPad, the cloud icon will not appear next to the movie. You will see the cloud icon next to the movie if you delete it from your iPad and then you can download it again. That is assuming that the movie is eligible for a free download as not every movie can be downloaded again for free.
    This is a screenshot of the purchased tab in iTunes on my iPad. These movies are already on my iPad so there is no cloud icon next to them for another download.
    What I don't understand is for what reason are you trying to access the movie? If you want to watch the movie, it is in the videos app. Maybe that is where I am missing the boat.

  • Is there a simple way to save and open attachments?

    When I save an attachment in mail, I have to hit save, navigate to the place to save, say OK, then to open it, I have to open a finder window, navigate (again) to the place, and open it. Because we have a very deep directory structure on our corporate drive, it takes like 20 seconds to do this every time I want to save an attachment. Isn't there an easier way to do this?
    SUGGESTION TO APPLE: why not put a button on the Save dialog that says "Save and Open".

    Just click on the attachment to open it, then if it's worth saving, press Save As...
    In the save window, you will see a list of recent places and if your lucky, one shows up that is on the mark and you can select it directly.

  • HT201371 Why isn't there an album ONLY for photos taken by camera?

    This is my first iPhone, and I find it extremely messy that my camera roll has so many pictures from everywhere:
    1- Pcitrues taken by camera
    2- Pictures saved through Safari
    3- Screenshots
    4- and maybe some pictures saved through other Apps.
    Why isn't there a way that all camera pictures can go into an album which no other app can use (including iOS' built-in apps such as Safari).
    In Android for example, it was much easier...

    It is just designed to use the computers ability to take off pictures as if it were a digital camera.
    While we are all users here and cannot explain why they chose this route, my assumption is that many people already organize photos via some 3rd party program (Mac users use iPhone, many Windows users use Picasa etc), so they had the Camera Roll act as a digital camera when plugged in to allow users to maintain the same process of getting pictures and organizing that they are already use to using. If one things about it, it was a nice thing they did and not force users to use iTunes to get pics, then have to go to their other program and have to manually organize.

  • Why isn't there a mobile version / screen size for apple support communities ?

    Why isn't there a mobile version / screen size for apple support communities ?

    GOD!
    I have the same problem and I've been trying to find a way around it for about...4 hours now. It's driving me nuts!
    Is there anything that can be done? Can I really not use the nano b/c of my operating system? that seems nuts to me!
    am totally bummed out as mine was a gift as well and now I have to return it...

  • I have two apple accounts and one of which has my music on. I would like to move this music to the other account. Is there a simple way of doing this?

    I have two apple accounts and one of which has my music on. I would like to move this music to the other account. Is there a simple way of doing this?

    There is currently no way to merge accounts.  The best option is to pick one, and use it consistantly.
    HTH.

Maybe you are looking for