<< Chapter < Page | Chapter >> Page > |
Beginning of the class named Prob05MyClassA
The class named Prob05MyClassA begins in Listing 3 .
Listing 3 . Beginning of the class named Prob05MyClassA. |
---|
class Prob05MyClassA implements Prob05X{
private int data;//instance variableProb05MyClassA(int inData){//constructor
System.out.println("Prob05");System.out.println("Put your first name here");
data = inData;}//end constructor |
This class implements the interface named Prob05X .
A private instance variable
Listing 3 begins by declaring a private instance variable of type int named data . As a private instance variable, it is accessible by any method or constructor defined within the class but is not accessible to methodsfrom outside the class.
The constructor
The constructor for the class is shown in its entirety in Listing 3 .
The constructor begins by displaying the problem number and the student's first name on the command line screen.
Then it assigns the value of the incoming parameter named inData to the variable named data . This makes that value available to the methods that are defined within the class.
The method named getModifiedData
We learned earlier that the class named Prob05MyClassA
With the exception of some very subtle differences (that are beyond the scope of this course) , that concrete definition must match the signature of the declared method.
Code for the method named getModifiedData
The method named getModifiedData is shown in its entirety in Listing 4 .
When this method is called, it
Listing 4 . The method named getModifiedData. |
---|
public int getModifiedData(){
return data - 1;}//end getModifiedData() |
The method named getData
We also learned earlier that the class named Prob05MyClassA
Code for the method named getData
The method named getData is shown in its entirety in Listing 5 .
This method returns a copy of the value stored in the variable named data .
Listing 5 . The method named getData. |
---|
public int getData(){
return data;}//end getData() |
A round trip
When the code in Listing 1 instantiates an object of the Prob05MyClassA class, it passes a random value as a parameter to the constructor.
The constructor shown in Listing 3 stores that random value in the instance variable named data .
When the method named getModifiedData is called, it returns a value that is the original random value less 1.
When the method named getData is called, it returns a copy of the original random value.
The toString method
The class named Prob05MyClassA extends the class named Object by default. It inherits a method named toString from the class named Object . The inherited method has very specific behavior.
Overridden toString method
The code in Listing 6 overrides the inherited method to provide a different behavior when the method is executed in conjunction withan object of the Prob05MyClassA class.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?