In the previous lesson you wrote a class to model a zookeeper.
Keywords this and super
The keyword this is used to refer to the current instance, sometimes known as the receiver object. You have previously used it when overloading constructors in order to delegate to a different constructor:
public Lion(String myName, String myGender) {
this(myName, myGender, 0);
}
The this keyword can also be used as a prefix to variables and methods using dot notation in order to explicitly specify that the variable or method refers to the current object. Look at the three-argument constructor in Animal:
public Animal(String myName, String myGender, int myAge) {
name = myName;
gender = myGender;
age = myAge;
dateAdmitted = new Date(); // today's date is assumed
}
You will note that the argument variable names are different to the instance variable names (e.g., the argument variable reference myName is assigned to the instance variable reference name).
However, it is quite permissible, and fairly common, for argument variable names to be given the same name as their instance variable counterparts; in this event, the constructor signature above would now look like this:
public Animal(String name, String gender, int age) {
If you make the above changes, you would naturally need to modify their assignment to their instance variable counterparts, but look how this would appear in the case of the name attribute:
name = name;
So which version of name is being referenced above, the argument variable or the instance variable? In fact, when an argument variable has the same name as an instance variable, the argument variable hides the instance variable, so the answer to the above question is that it is the argument variable name which is being referenced, and the instance variable is not touched at all.
The solution is to prefix the keyword this to the variable name using dot-notation in order to tell Java to use the instance variable. Therefore, change the above statement to:
this.name = name;
What the above will do is assign the argument variable reference name to the instance variable reference name; the this keyword lets you distinguish between the two.
Now modify the remainder of the constructor so that is looks like this:
public Animal(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
dateAdmitted = new Date(); // today's date is assumed
}
Note the following:
- If the keyword
thisis not used, then Java refers to the local argument variable instead of the instance variable - If there is no local argument variable having the same name as an instance variable then this is implied and is not required to be specified, such as
dateAdmitted, above
You could also change the setName() methods argument name to be the same as the instance variable name, requiring the use of the this keyword:
public void setName(String name) {
this.name = name;
}
public void setName(String name, String nickName) {
setName(name + " (also known as " + nickName + ")");
}
And you could change the setGender() and setAge() methods to do the same, although this would only be for the sake of consistency – not something required by Java.
public void setGender(String gender) {
this.gender = gender;
}
public void setAge(int age) {
this.age = age;
}
Note that you could change the getter methods to specify the this keyword as follows:
public String getName() {
return this.name;
}
However, remember that in the absence of an argument variable with the same name the this keyword is not required, and normal practice is not to include it unless it is required.
The super keyword
The keyword super is used when you want to refer to an instance variable, instance method or constructor that is in a superclass of the current instance. You have previously used super in the Lion, Monkey and Penguin classes so that they invoke the correct constructor of their superclass, Animal. For example, in Lion:
public Lion(String myName, String myGender, int myAge) {
super(myName, myGender, myAge);
}
In constructors, a call to super() (passing no arguments) is implied if you do not specify it, so the three-argument ZooKeeper constructor could be written as:
public ZooKeeper(String name, String address, String email) {
super();
this.name = name;
this.address = address;
this.email = email;
}
Note the following:
super()invokes the no-argument constructor of the superclasssuper()is implied; if you don't specify it Java because will invoke it for you- You can pass arguments to
super()as long as there is a constructor taking that combination of arguments in the superclass - If your constructor invokes
this()thensuper()cannot also be specified - If you specify either
this()orsuper()then it must be the first statement inside the constructor
It is also common to use super when overriding a method. For example, suppose in class Lion you decide to suffix the lion's name with some additional text; you could override the getName() method as follows:
... Inside the Lion class ...
@Override
public String getName() {
return super.getName() + " the lion";
}
The statement super.getName() invokes the getName() method of this class's superclass (which in this case, is Animal). The String that returns (such as "Leo"). then has the text " the lion" appended to it. Therefore, if you invoke the getName() method upon an object of type Lion it will return "Leo the lion" rather than just "Leo".
The application developed in this course does not require the getName() method of Lion to be overridden, so please remove the above code if you entered it.
In the next lesson you will write a class to model a visitor to the zoo.
Comments