Results 1 to 12 of 12

Thread: More Java questions

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    zombologist Senior Member doc_bean's Avatar
    Join Date
    Oct 2004
    Location
    Riding Shai-Hulud
    Posts
    5,346

    Default Re: More Java questions

    Quote Originally Posted by Blodrast
    I'm not sure the questions were very clear to me (not necessarily your fault, no worries - I'm tired and other things), but I'll give it a shot.

    1.
    What exactly is the problem ?
    Also, can you modify any of the interfaces of the "given" classes (the other 3, I presume) ?
    Wouldn't evey subclass have its own get/set methods for handling the name ? So, if the object you're calling setName() for is a Link, the appropriate method from the Link class will be invoked. You don't have to do anything special for this.

    Are you asking how to create a constructor for the Link class, that does take a reference as an argument ? Or what exactly ? Sorry, I don't get it.
    The problem was that I wanted 1 class with 2 name conventions, 1 for links to directories and 1 for links to files. I've now just made 2 subclasses and made the Link class abstract.

    SetName() was handled solely in DiskItem and name was a private variable. I used isValidName() with an override in the subclasses to check if a name could be given.

    The real problem was the I had to invoke the superconstructor first in my subclassconstructer (as always in java i believe) which meant I couldn't give data relating to the reference (no references in the superconstructor) to the constructor.

    This probably doesn't make too much sense either


    2.
    Polymorphism allows you to invoke the right type of method for the appropriate type of object - you don't have to do anything to get that functionality.
    For instance, assume you wanna call setName() for some DiskItem, which is actually a Link. Then Link.setName() will be called, because the object actually belongs to the Link class.
    You can also define virtual methods in the DiskItem class.

    From what I've seen, a common approach is to define the get/setName methods in the DiskItem class as abstract methods - e.g., String getName (DiskItem x) = 0; (or return false, or some error code).
    This will force people to define them in the subclasses, for the appropriate type of objects.

    I really don't know if any of this was any help, if you can be more specific, it would help.
    yes I use a similar method except that all variables (for example this.name) have to be declared private, so I can't use a setName() if it doesn't invoke its super-method. I got an 'acceptable' solution that way.

    Good luck anyway !
    Thanks, and thanks for your help !

    edit: oh, I forgot to mention one thing: if you have a DiskItem object, there are methods that check if you can downcast it to a subclass type (IF it is, indeed, of that class type). I don't remember the class name and the methods, though. But this may be what you want - of course, you can always downcast it yourself if you know that the DiskItem is in fact a Link.
    'instanceof' ?

    I'm forced to use that quite a bit
    Yes, Iraq is peaceful. Go to sleep now. - Adrian II

  2. #2

    Default Re: More Java questions

    okay, cool, I guess I don't have anything more to add - glad you figured it out ! :)
    Therapy helps, but screaming obscenities is cheaper.

  3. #3
    zombologist Senior Member doc_bean's Avatar
    Join Date
    Oct 2004
    Location
    Riding Shai-Hulud
    Posts
    5,346

    Default Re: More Java questions

    thanks, I have another project comming this week, so I'll probably be back
    Yes, Iraq is peaceful. Go to sleep now. - Adrian II

  4. #4

    Default Re: More Java questions

    I'll be around :D
    Therapy helps, but screaming obscenities is cheaper.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Single Sign On provided by vBSSO