How to create a method on an object that can later be used by similar objects:

 

Demo   Code 

 

v     Rather than writing a separate method for each object and then calling the method for each specific object, we write one method that takes an object as a parameter.  When ever we want some object to perform the action, we pass that object as an argument to the method. 

 

v     The method must also contain variables to hold the object-parts of the object parameter, since at the time we when write the method we can not specify the body part of the specific object that will perform the action (since it could be a different object on any call to the method). 

 

v     If you need to get a value or object from one method, say method-1, to another method (method-2), any variable or parameter of method-1 can be passed in the call to method-2, provided that method-2 has a parameter of that type.  For example, in the code for the demo, the method   world.jumpAndWave has one object parameter (whichWaver).  In the body of the world.jumpAndWave method, whichWaver is passed as the argument to the sunWorshiper.jump method’s one object parameter (whichWorshiper).  In the body of sunWorshiper.jump, whichWorshiper is passed as the argument to the sunWorhiper.prepare2Jump method’s object parameter (whichJumper).