|
-
Mar 14th, 2007, 08:16 PM
#2
Re: Inheritence issue?
It can't find workOnPart because you've declared the variable as an Employee, which doesn't contain that method. You need to cast it to a FactoryWorker.
Code:
((FactoryWorker)myEmployee).workOnPart(...);
But that's not good; a sign of bad design. The usual way is to make methods for dealing with specific employee types and type their arguments using the specific type.
Code:
void tellThatGuyToGetOffHisButtAndDoSomeWork(FactoryWorker guy, String whatToDo)
{
guy.workOnPart(whatToDo);
}
That way, there's no explicit casting involved; you can code the method without worrying about types; and any type checking errors will be reported in a more logical place in your code.
Obviously, that's a contrived example: you hopefully wouldn't need to make a method that only did that.
As a side note, declaring variables without initialising them is generally a bad practice. You should only declare a variable when you can give it a meaningful value. Otherwise, unless you're the most meticulous (and unproductive) coder in the world, you'll run into some sort of error related to an uninitialised variable.
Last edited by penagate; Mar 14th, 2007 at 08:21 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|