How do I recover the actual type of the this object in a class hierarchy?

Hi,
This is code snippet from Angelica Langer's FAQ:
abstract class Node <N extends Node<N>>  {
     private final List<N> children = new ArrayList<N>();
   private final N parent;
   protected Node(N parent) {
        this.parent = parent;
        if(parent!=null)
           parent.children.add(this);  // error: incompatible types
   public N getParent() {
     return parent;
   public List<N> getChildren() {
     return children;
}There are 3 different approaches available in the FAQ to solve this problem.
My question is:
If I write:
parent.children.add((N)this);  // cast added here
...as described at the end of this topic in the FAQ, am I safe here (despite
the compilers warning) ? In my opinion, although I understand that
compiler needs to issue a warning, it is actually 100% safe .... or is it not ?
Or let's ask more precise question: can it throw a ClassCastException at
runtime ever ? If so, can you give any example ?
Thanks,
Adrian

You might not get a CCE within the Node class (because of erasure), but maybe on accessing the children, e.g., in a loop, where the cast will be created by the compiler, based on the type information you give to it.
class A extends Node<A> {
  public A(A parent) { super(parent); }
class B extends Node<A> {
  public B(A parent) { super(parent); } // This will add a B as child to an A!
A a = ...;
B b = new B(a);
for (A anA : b.getChildren()) { ... }Creating an instance of B will add it as child to a List<A>, which will most probably result in a CCE when looping on the list like shown above, which is type-safe from the compiler point of view.

Similar Messages

  • I downloaded an Adobe file from my email but cannot find it on my hard drive or in recycle bin - it's in downloads but am unable to open from downloads - how can I recover the file?

    I downloaded an Adobe file from my email (service provider is MWeb) but now cannot find it on my hard drive and it's not in the recycle bin. It does, however, appear in Firefox downloads but I'm unable to open it from there. How can I recover the file?

    Thanks Rob.
    I actually had no clue what the core problem was caused by. So it's Windows itself...that helps some.
    I'll resubmit there. It might actually have saved time for it to simply have been moved to there instead of off-topic, though I suppose you have to follow protocol.

  • I had a power mac g4 into which i put 3, 320 gb PATA hard drives in a raid slice config so that it worked as one drive needless to sat that i lost the g4 to a surge but the drives are good. now i have an imac, how can i recover the info off those drives

    i had a power mac g4 into which i put 3, 320 gb PATA hard drives in a raid slice config so that it worked as one drive needless to sat that i lost the g4 to a surge but the drives are good. now i have an imac, how can i recover the info off those drives. can i put the drives in external cases and plug them all in, will the imac see them as a raid slice then  help please

    Before you have another accident:
    Buy a UPS of good quality and sufficient to your needs.
    I would have to assume that the drives were connected to a PCI PATA card, hopefully. Otherwise, well RAID and having drives on the same bus (master and slave).
    And no backup, none at all...
    Get your hands on a G4.
    Data Rescue 3 from Prosoft maybe.
    If they were SATA and running on PCI SATA controller, very popular and common really in G4s, more options would be open.

  • I have forgotten my Password to be able to connect between my desktop Mac and Mac Book on my home network. How can I recover the password or change it?

    I have forgotten my Password to be able to connect between my desktop Mac and Mac Book on my home network. How can I recover the password or change it?

    The password used to log in to a user account remotely is the same as you would use to log into it locally. You can also use your Apple ID, so try that.
    To change your login password read OS X Mavericks: Reset a login password
    also OS X Mavericks: If you forget your login password

  • HT204053 I am using my Apple ID for me and for my wife. when I sync. the phones i lost all my wife contact. how do i recover the contact from i cloud?

    I am using my Apple ID for me and for my wife. when I sync. the phones i lost all my wife contact. how do i recover the contact from i cloud?

    While I have a few ideas, I think you'd have better luck troubleshooting this in the iCloud forum.

  • I gave my father a mini ipad and he forgot the icloud password and the e mail for recovering it.  How can I recover the password or create a new one if I have no idea at all of the info he configurated.?? ;(

    I gave my father a mini ipad and he forgot the icloud password and the e mail for recovering it.  How can I recover the password or create a new one if I have no idea at all of the info he configurated.?? ;(

    He will have to contact Apple and speak to someone in account security if he can't recover it at https://iforgot.apple.com/appleid

  • I have an iCloud account that is different from my apple account.. How can I recover the password for that account??

    I have an iCloud account that is different from my apple account. How can I recover the forgotten password

    Hello Bgenie
    If you have forgotten the password to that Apple ID, then go to My Apple ID and you will see and option to reset your password. You will need to answer the security questions or if you have a rescue email address set up you can get it sent to that email to reset the password. Check out the article for more information.
    Apple ID: If you forget your password
    http://support.apple.com/kb/ht5787
    Regards,
    -Norm G.  

  • I want to setup Lightroom on my new laptop.  I have my latest serial number but it is the upgrade serial number for LR5.  I do not have the original serial number from my purchase of LR3.  How can I recover the serial number from my original purchase of ?

    I want to setup LR on my new laptop.  I have the serial number from my latest purchase but not the original purchase of LR3.  How can I recover the serial number from my original LR purchase?

    If you did register it back then, open your Adobe account with your Adobe ID and you will find it there: Find your serial number quickly
    If you still have it installed, open the System info in Help menu or look here:
    C:\ProgramData\Adobe\Lightroom\Lightroom xy Registration.lrreg

  • How can I recover the itunes tool bar? It disappeared. I cannot use "FILE" to create discs or playlists,

    How can I recover the itunes toolbar. I have no "FILE" to create discs & playlists, and no "X" or "-" to close or minimize itunes.

    did you reselve this? I have same problem

  • How do I recover the music I have purchased after a Windows disaster?

    How do I recover the music I have purchased after a Windows disaster? Windows on this computer stopped working correctly. I was told by HP support that the registry was corrupted and that I had to re-install Windows and programs from CDs that came with the computer. I did this and Windows started working correctly again. The only trouble is that all of the music I bought is gone. How do I recover the music I have purchased from iTunes after a Windows disaster and after re-installing Windows?
    Please tell me that you know about all of my song purchases and that there is a way I can download all of these to this computer again without having to pay for them again.
    I have learned my lesson about Windows. In the short run I plan to buy a backup drive that I can plug into this computer that I can restore from if and when something like this happens to Windows again. In the long run I plan to buy an Apple computer the next time I buy a computer.

    You could try contacting the iTunes Music Store Customer Service and you may be able to persuade them to sanction a second free download however they are not under any obligation to do so. The policy on lost songs is that you have to pay to download them again:
    "Once a Product is purchased and you receive the Product, it is your responsibility not to lose, destroy, or damage the Product, and Apple shall be without liability to you in the event of any loss, destruction, or damage."
    You can email them from any of the links on this page: iTMS Customer Service

  • How can i recover the deleted photos on my Xperia S ?

    I deleted some photos carelessly a few days ago. Since i use the phone model is "Xperia S" which is use of interal storage hard disk (not have SD card select), i have tried use some of recovery software (like CardRecovery, Pandora Recovery) but since Xperia S connect the PC via USB only have Media Transfer Mode (MTP), the aforesaid software cannot detect my phone as an external driver and hence cannot recover any files. Please help me to solve this problem.

    How can i recover the deleted photos on my Xperia S ? I want to recover deleted  photos from my mobile for a particular number. All the  photos are deleted instantly and no threads were present on my mobile so I need to recover every  photos individually.Your experience is so similar with mine! I ever deleted a  photos sent by my GF, which is so meaningful for me. I was so regretful, then I was crazy to find solution to recover this sms from my phone. At last, I got a recommedation from a Android forum, it said that a data recovery software for Android can recover deleted  photoson my Xperia S , so I tried to download and use it. Awesome! It recover the sms! You can have a try:Follow the steps below to recover the deleted photos  on my Xperia S . Have a try. 1 Connect your Xperia S and enable USB debugging2 Analyze your Android phoneAfter the USB debugging, reconnect your phone if you are asked to disconnect it during the setting. Then can see the window below. Here you need to bear in mind that your phone's battery is more than 20% charged, and then you can click "Start" to let the program analyze your Android phone. 3 Scan your Android for deleted text photos4 Preview and recover photos from Android You can preview the photos that can find from it. Related Articles:
    How to recover deleted datas from Android phone ?
    Download the free trial version of Android photos Recovery and have a try. Click here to downloadHow to recover deleted photos on your Xperia S?

  • HT1766 I recently replaced my iphone and took 6 weeks of new pictures, I plugged it to my computer and by accident I restored the back from 4 months ago and erased my new pictures. How can I recover the new pictures ?? Help!!

    I recently replaced my iphone and took 6 weeks of new pictures, I plugged it to my computer and by accident I restored the back from 4 months ago and erased my new pictures. How can I recover the new pictures ?? Help!!

    Sorry those pics might be gone, since you restored from your backup.
    Only way possible if by some possibility you were able to backup your current data to itunes, then you can restore from that backup.
    Open itunes - edit - preference - devices - backup - see if there is an up to date backup.
    If you don't have one then sorry those pics are gone.

  • HT4623 lost all my pictures and videos while updating ios7.1.1. i dont have backup to restore. how can i recover the data back?

    lost all my pictures and videos while updating ios7.1.1.
    i dont have backup to restore. how can i recover the data back?

    You should never update without making sure that you have saved everything on your computer first.
    You should import all pics and vids before attempting any update.
    If you have failed to save the data, then it is gone

  • I resently upgraded my OS to Snow Leopard, but the information in my address book did not transfer.  How can I recover the lost information?

    I upgraded to Snow Loepard OS.  But not all the information in the address book was transfered.  How can I recover the lost information from my old address book?

    No I did not make a copy of my address book. 
    Some information was moved over, many did not.
    I do not see a pattern.
    I think all the info is still on the hard drive, I just don't know how to recover it.

  • How do i recover the answers to my security questions?

    How do i recover the answers to my security questions?

    Forgotten Security Questions/Answers
    You need to contact Apple by:
    1 - Use the Express lane and start here:
    https://expresslane.apple.com
    then click More Products and Services>Apple ID>Other Apple ID Topics>Forgotten Apple ID security questions.
    or
    Apple - Support -form iTunes Store - Contact Us
    2 - Call Apple in your country by getting the number from here:
    http://support.apple.com/kb/HE57
    or           
    Apple ID: Contacting Apple for help with Apple ID account security
    3 - Use your rescue email address if you set one up
    Rescue email address and how to reset Apple ID security questions
    For general  information see:
    Apple ID: All about Apple ID security questions

Maybe you are looking for

  • Suggestion: Oracle text CONTEXT index on one or more columns ?

    Hi, I'm implementing Oracle text using CONTEXT ..... and would like to ask you for performance suggestion ... I have a table of Articles .... with columns .. TITLE, SUBTITLE , BODY ... Now is it better from performance point of view to move all three

  • ECC 6. 0 - How to include translations into a change requests

    Hello everybody, we are developing in ECC 6.0 and we are experiencing the following problem: when mantaining the text elements of custom programs into the required languages, the system doesn't ask to type in or create any change request. It just sav

  • 4402 - Guest Access no longer working

    When this started, I cannot be sure, but it has been working for sometime now and cannot understand where the problem is. There is one guest interface, one guest WLAN. The WLAN is set with a DHCP override address of the guest interface. But it no lon

  • Wondering outloud - 2013 13' Air or 13' Pro

    Hello, I am looking at buying a Mac laptop to keep current on emerging coding tools, standards, etc.. XCode HTML 5 Java Development etc.. This machine wouldn't be a full fledged desktop replacement, just something to work on when away from the office

  • Issues in creating Sales Document Type

    I am creating a new Sales Document Type, but the problem is this that the "Probability"  & "Incompl. Proced"  fields are disabled and I am unable to enter record in this field , please help me out that how can I enter values in these fields.