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

what is microbiology
Agebe Reply
What is a cell
Odelana Reply
what is cell
Mohammed
how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
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