- The
this
keyword has two meanings: to refer a field of the implicit parameter and to call another defined constructor in the same class. Thesuper
keyword also has two meanings: to call a superclass constructor and call a superclass method. Like usingthis
keyword to call a constructor, the constructor call must be the first statement in the new constructor. In the following example,Manager
is the subclass ofEmployee
, theEmployee
class which is hidden for simplicity does not have the fieldbonus
:public Manager(String name, double salary, int year, int month, int day) { super(name, salary, year, month, day); //is the same as Employee(name, salary, year, month, day) bonus = 0; }
Likewise, the
this
keyword can be used in a similar way. - If a subclass reference is assigned to a superclass variable, you are promising less, and the compiler will simply let you do it, for example:
Employee e; e = new Manager(. . .);
If a superclass is assigned to a subclass variable, you are promising more:
Manager e; e = new Employee(. . .); //this doesn't work!
In this case, you must use a cast so that your promise can be checked at runtime.
- Now consider the following situation:
Manager boss = new Manager(...); boss.setbonus(5000); var staff = new Employee[3]; staff[0] = boss; //this works staff[1] = new Employee(...); staff[2] = new Employee(...);
However, staff[0] will be an
Employee
instance whose actual type (Manager
in this case) has been temporarily forgotten and we cannot dostaff[0].setbonus(xxx)
(although we can still doboss.setbonus(xxx)
). To convert it intoManager
again, we can cast it like this:Manager xx = (Manager) staff[0]
. And this should be the only case we want to make a cast for an object: to use an object in its full capacity after its actual type has been temporarily forgotten. - Use the
instanceof
operator to check if a cast will succeed before doing it:if (staff[1] instanceof Manager) { boss = (Manager) staff[1]; //... }
-
Converting the type of an object by a cast is not usually a good idea, so avoid it if possible.
- The
final
modifier can be used on classes to prevent someone from forming a subclass of one of your classes. You can also make a specific method in a class final, then no subclass can override that method. (All methods in a final class are automatically final.) Fields can also be declared as final. A final field cannot be changed after the object has been constructed. However, if a class is declared final, only the methods, not the fields, are automatically final.