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

  • must provide a concrete definition of the method named getModifiedData ,
  • because that method is declared in the interface named Prob05X ,
  • which is implemented by the class.

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

  • subtracts a value of 1 from the value stored in the instance variablenamed data , and
  • returns that modified value.
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

  • must provide a concrete definition of the method named getData,
  • which is also declared in the interface named Prob05X .

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.

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