|
Objects communicate with other object by invoking its methods. To call an object's method, you need to simply append the method name to an object reference with a period, “.”, and provide the arguments (if any) to the method within round brackets. If the method does not require any arguments, just use empty brackets. Following is the general format for invoking method:
objectReference.methodName(argumentList);
OR
objectReference.methodName();
You can obtain object reference by creating an object first. We will see how to create an object and how to store its reference in the next section. Note that there are two Java keywords this and super that represent special kinds of object references. When used within a class, this keyword refers to the current instance of that class. For example, if you use this keyword inside a method, then whenever you invoke that method on some instance of that class, this refers to that instance. The keyword super refers to the inherited part of the current instance. For example, if CheckingAccount is a subclass of Account class and if you wish to call withdraw() method of Account class from within the CheckingAccount class, you can do so as- super.withdraw(50). There are certain rules of using these two keywords inside constructors, which we will see later in this chapter as well as in Chapter 11.