Student
/ \
UGStudent GradStudent
/ \
PhDStudent
MSStudent
[ FieldModifiers ] Type FieldName1 [
=
Initializer1 ] ,
FieldName2 [ = Initializer2 ]
...;
public | protected | package | private | |
The class itself | Yes | Yes | Yes | Yes |
Classes in the same package |
Yes | Yes | Yes | No |
Subclasses in a different package |
Yes | Yes | No | No |
Non-subclasses in a different package |
Yes | No | No | No |
void methodB(int i)
void methodB(float f)
Note that if the toString() method were not provided for the class Coin, a default version would be called, that returns a string containing the name of the class; in this case Coin. This because every class is derived implicitely from Object class that has a method called toString. But sisnce we want to print either "Heads" or "Tails" we did provide a version that overrides the one given in Object class.
For Example: in FlipRace program, in the two statements:
System.out.print ("Coin 1: " + coin1);
System.out.println (" Coin 2: " + coin2);
The toString method of the class Coin is called implicitely. In fact, when an object is used as an operand of the string concatenation operator (+), that object's toString method is automatically called. It is also called if an object is sent to a print or println by itself. If no toString method is defined, the default version of Object toString is called.