<< 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:

  • If your class overrides a method in a superclass, you can use the super keyword to bypass the overridden version in the class and execute the version in the superclass.
  • If a local variable in your method or a member variable in your class hides a member variable in the superclass (having the same name), you can use the super keyword to access the member variable in the superclass.
  • You can also use super in a constructor of your class to call a parameterized constructor in the superclass.

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.

Questions & Answers

how did the oxygen help a human being
Achol Reply
how did the nutrition help the plants
Achol Reply
Biology is a branch of Natural science which deals/About living Organism.
Ahmedin Reply
what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
cell is the smallest unit of the humanity biologically
Abraham
what is biology
Victoria Reply
what is biology
Abraham
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask