Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

how to call a similar name method of a different class?  RSS feed

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,  have  a  problem.
i need to create a new City object which contains the variable" residents " in it.
i have a method in the country class which calls : int getNumOfResidents();
and i have a method in the City class which calls : long getNumOfResidnets();
now, i  want from my Country class, to call the getNumOfResidents() from my City class.
but i don't know how, because they have the same names.
how can i call it from the city class ( i can't change any of these method names)?
Thank you
 
Bartender
Posts: 5627
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The type of object determines which method gets called.
 
Marshal
Posts: 63314
205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can't you change the method names? That sounds like a bit of bad design or a poorly‑thought‑out assignment.

Assuming your City and Country classes are distinct, and ignoring the fact that the population of Chin may cause an overflow error with an int in thirty years, why is there a problem?If you call such a call from inside the Country object, you would have some way to find the city. Maybe you have a List<City>Because you call the method on the object reference, there is no problem with disambiguation.
 
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!