<< Chapter < Page | Chapter >> Page > |
Make a callback
Then the main method sends a message to objB asking it to use the saved reference to make a callback to objA . The code in the method named callHimBack uses the reference to objA saved earlier to call the method named callMeBack on objA , passing 15 as a parameter. The method named callMeBack belonging to objA saves that value in an instance variable.
Show the data
Finally, the main method calls the showData method on objA to cause the value stored in the instance variable belonging to objA to be displayed on the computer screen.
Callbacks are important
Again, this program is provided solely to illustrate the concept of a callback using the this keyword. In practice, callbacks are used throughout Java, but they are implemented in a somewhat more elegant way, makinguse of interfaces.
For example, interfaces with names like Observer and MouseListener are commonly used to register observer objects on observable objects (sometimes referred to as listeners and sources). Then later in the program, when something of interest happens on the observable object (the source), all registered observer objects (the listeners), are notified of the event.
The main point regarding the this reference
The main point of this discussion is that the this reference is available to all instance methods belonging to an object, and can be usedwhenever there is a need for a reference to the object on which the method is called.
To disambiguate something
At least one prominent author uses the word disambiguate to describe the process described by the first item in the earlier list , where the this keyword is used to bypass one variable in favor of a different variable having the same name. I will also usethat terminology in the following discussion.
Three uses of the super keyword
Here are three common uses of the super keyword:
The program named Super3
The program in Listing 4 uses super to call a parameterized constructor in the superclass from the subclass constructor. This is animportant use of super .
The program also uses this and super to disambiguate a local variable, an instance variable of the subclass, and an instance variable of thesuperclass. All three variables have the same name.
Listing 4 . The program named Super3 . |
---|
/*File Super3.java
Copyright 2002, R.G.BaldwinIllustrates use of super reference to
access constructor in superclass. Alsoillustrates use of super to
disambiguate instance variable insubclass from instance variable in
superclass. Illustrates use of thisto disambiguate local variable from
instance variable in subclass.Tested using JDK 1.4.0 under Win2000
The output from this program is:In SuperClass constructor.
Setting superclass instance var to 500In subclass constructor.
Setting subclass instance var to 400In main
Subclass instance var = 400In method myMeth
Local var = 300Subclass instance var = 400
SuperClass instance var = 500**************************************/
class SuperClass{int data;
//Parameterized superclass// constructor
public SuperClass(int val){System.out.println(
"In SuperClass constructor. ");System.out.println(
"Setting superclass instance "+ "var to " + val);
data = val;System.out.println();//blank line
}//end SuperClass constructor}//end SuperClass class definition
//===================================//class Super3 extends SuperClass{
//Instance var in subclass has same// name as instance var in superclass
int data;//Subclass constructor
public Super3(){//Call parameterized SuperClass
// constructorsuper(500);
System.out.println("In subclass constructor.");
System.out.println("Setting subclass instance var "
+ "to 400");data = 400;
System.out.println();//blank line}//end subclass constructor
//---------------------------------////Method illustrates use of this and
// super to disambiguate local// variable, instance variable of
// subclass, and instance variable// of superclass. All three
// variables have the same name.void myMeth(){
int data = 300;//local variableSystem.out.println(
"In method myMeth");System.out.println("Local var = "
+ data);System.out.println(
"Subclass instance var = "+ this.data);
System.out.println("SuperClass instance var = "
+ super.data);}//end method myMeth
//---------------------------------//public static void main(
String[]args){
Super3 obj = new Super3();System.out.println("In main");
System.out.println("Subclass instance var = "
+ obj.data);System.out.println();//blank line
obj.myMeth();}//end main method
}//End Super3 class definition. |
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?