This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes equals() override in different class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equals() override in different class" Watch "equals() override in different class" New topic
Author

equals() override in different class

Jason Job
Greenhorn

Joined: Sep 26, 2011
Posts: 17
I have 4 classes that are relevant to my question.

Super Employee (Calls for 4 variables)
Child ProductionWorker (Calls for 4 previous + 1 additional)

WorkForce (Generates ArrayList of 20 production workers using randomized variables.) (Also has a getter for a random production worker)



Shift (Needs to pull a random ProductionWorker out of ArrayList and add it to a new ArrayList)


My question : How do I make this thing in shift work? I am trying to assign a variable = variable.getWorker(); and all it does is say that I can't call getWorker since it's not of the ProductionWorker class. I can't put getWorker in productionworker because there's no arraylist. Confused!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

Since the getWorker() method is in the WorkForce class, you must call it on a WorkForce object. You are trying to call it on a ProductionWorker object, and that's what the Java compiler complains about.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jason Job
Greenhorn

Joined: Sep 26, 2011
Posts: 17
Managed to figure it out. Now I'm having issues with this :



Throwing up an error asking for all the fields of ShiftSupervisor (name, number, etc), is there any way to just copy this object over? I have method .getSupervisor() returning 1 of several possible objects of this type.
Jason Job
Greenhorn

Joined: Sep 26, 2011
Posts: 17
Resolved that as well ... next issue!



Without having to give the rest of the code, the bolded statement is giving me an error saying it cannot find the symbol. I have an overridden equals method in both employee (superclass of shift supervisor) and in shiftsupervisor. Can't get it to lie down.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18159
    
    8

Yes, that's wrong, and the compiler tells you so. But there's no point in asking other people what you should do instead without telling them why you feel you have to see if shiftWorkers is equal to something. What are you trying to do there?
Jason Job
Greenhorn

Joined: Sep 26, 2011
Posts: 17
There are six employees in this array, with two different subclasses. Supervisor and Worker, I'm looking to find out which one is equal to Supervisor. I ended up just using an instanceof and getting my true / false variable from that. However I would like to know best how to use an equals override to do something similar.

Or better yet, how to use an equals method to compare 30 Employees to see if any of them have the same name.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18159
    
    8

Jason Job wrote:There are six employees in this array, with two different subclasses. Supervisor and Worker, I'm looking to find out which one is equal to Supervisor. I ended up just using an instanceof and getting my true / false variable from that. However I would like to know best how to use an equals override to do something similar.

Or better yet, how to use an equals method to compare 30 Employees to see if any of them have the same name.


But you don't want to find out if an employee is "equal to" Supervisor. That isn't a meaningful concept. You want to find out if an employee IS A Supervisor; that's why you use "instanceof".

As for comparing two employees to see if they have the same name, you would compare their names and see if they are equal.
Jason Job
Greenhorn

Joined: Sep 26, 2011
Posts: 17
Paul Clapham wrote:
Jason Job wrote:There are six employees in this array, with two different subclasses. Supervisor and Worker, I'm looking to find out which one is equal to Supervisor. I ended up just using an instanceof and getting my true / false variable from that. However I would like to know best how to use an equals override to do something similar.

Or better yet, how to use an equals method to compare 30 Employees to see if any of them have the same name.


But you don't want to find out if an employee is "equal to" Supervisor. That isn't a meaningful concept. You want to find out if an employee IS A Supervisor; that's why you use "instanceof".

As for comparing two employees to see if they have the same name, you would compare their names and see if they are equal.


Suppose I have an ArrayList named screwTypes, how would I compare each one of their names to ensure that they are not equal? Have a loop that starts at 0, and then iterates over the the entire array (skipping itself) looking for something that .equals itself? then the index increases and it does the entire thing once again? Would it be possible for anyone to give me a sample code of something similar?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals() override in different class
 
Similar Threads
Two different subclass objects within an arraylist.
Sending ArrayList from one class to another, not passed as parameter
Frequency of used object in ArrayList
Null Pointer Exception
[Solved] toString() issues