Author | Message |
---|---|
rabbott
Posts: 1649
|
Posted 12:07 Oct 16, 2013 |
Yesterday we talked about an example that looked something like this. public class Time /** /** public int getHours() public int getMinutes() /** public String toString( ) public static void main(String[] args) Running main() produces this result. timeA timeB People sometime find the method giveTime() confusing. That method calls the method acceptTime() in another object. The potential confusion is that the two methods are defined in the same class, i.e., the Time class. So how can one object run the method giveTime() which asks a second object to run the method acceptTime() if both methods are in the same class? The answer is that all Time objects have all the methods defined in the Time class. One way to think about it -- although this is not literally correct -- is to imagine that when you execute Time timeA = new Time(9, 30); the entire Time class is copied and given to timeA. Then when you execute Time timeB = new Time(8, 23); the class is copied again and given to timeB. So the line timeA.giveTime(timeB); asks the timeA object to execute its giveTime() method.That method asks the timeB object to execute its acceptTime() method. Since each object has access to both methods, there is no problem.
Last edited by rabbott at
12:28 Oct 16, 2013.
|