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.Originally Posted by Blodrast
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![]()
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.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.
Thanks, and thanks for your help !Good luck anyway !
'instanceof' ?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.
I'm forced to use that quite a bit![]()
Bookmarks