Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

Can we over-load "+" in Java?

Thread view: 
Shawn - 14 Sep 2006 14:14 GMT
Hi,

Suppose I have following classes:

class Person {
...

}

class Team {
    //how to have a member variable which holds an array of objects of Person?

...
}

Person person1= new Person("John Smith");
Person person2= new Person("Linda King");
Person person3= new Person("Ed Johnson");

//here is what I want:
Team BestTeam;
BestTeam = person1 + person2;
//now an object of class Team was created and this team has the two people

BestTeam = BestTeam + person3;
//now Ed Johnson joined the team

BestTeam = BestTeam - person3;
//now Ed Johnson left the team

Can we overload "+" and "-" to achieve the effect above? If not(which is
my feeling), how can we achieve something similar? If we use a method
(say,join(), left()), please give me full implementation of the method?
Patricia Shanahan - 14 Sep 2006 15:56 GMT
> Hi,
>
[quoted text clipped - 30 lines]
> my feeling), how can we achieve something similar? If we use a method
> (say,join(), left()), please give me full implementation of the method?

Java does not allow user-defined overloading of operators. Even if it
did, I would argue against using + for anything other than addition of
numbers, - for anything other than subtraction of numbers etc. I've seen
C++ code that uses operators for arbitrary functions, and find it quite
unreadable.

For this sort of task in Java, I would design a class Team with methods
such as "add", "remove", "isMember", and possibly a method returning an
iterator over the members.

Team bestTeam = new Team();
bestTeam.add(person1);
bestTeam.add(person2);
bestTeam.add(person3);
bestTeam.remove(person3);

etc.

Inside bestTeam, I would use e.g. a java.util.Set, with implementing
class chosen based on the sort of operations, to remember the current
members.

Patricia
Shawn - 14 Sep 2006 16:13 GMT
> Java does not allow user-defined overloading of operators. Even if it
> did, I would argue against using + for anything other than addition of
[quoted text clipped - 19 lines]
>
> Patricia

Thank you very much. You have solved the doubts in my mind.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.