<< Chapter < Page Chapter >> Page >

Cast to type byte

This value is cast to type byte , which discards all but the eight least significant bits of the int value. When it is stored in the variable named randomNumber of type int , the sign is extended through the most significant 24 bits and it becomes a value of type int that isguaranteed to be of relatively small magnitude.

Why cast to byte?

I cast the random value to type byte simply to cause the values that are displayed to be smaller and easier to compare visually.

Instantiate an object of type Prob04MyClass

The next statement in Listing 1 instantiates an object of the class named Prob04MyClass , passing the random value as a parameter to the constructor. At this point, I will put the explanation of the class named Prob04 on temporary hold and explain the class named Prob04MyClass , which begins in Listing 2 .

Listing 2 . Beginning of the class named Prob04MyClass.
class Prob04MyClass extends Prob04{ private int data;public Prob04MyClass(int inData){//constructor System.out.println("Prob04");System.out.println("Dick"); data = inData;}//end constructor

Extends the abstract class named Prob04

First note that the class named Prob04MyClass extends the abstract class named Prob04 .

Among other things, this means that either this class must override the abstract method named getData that was declared in the superclass, or this class must also be declared abstract.

Does it override getData?

Seeing that this class isn't declared abstract, we can surmise at this point that it does override the abstract method named getData . We will see more about this later.

Beginning of the class named Prob04MyClass

The class definition in Listing 2 begins by declaring a private instance variable of type int named data . Note that it does not initialize the variable. Therefore, the value is automatically initializedto an int value of zero.

The constructor

Then Listing 2 defines the constructor for the class. The first two statements in the constructor cause the first two lines of textshown in Figure 1 to be displayed on the command line screen.

Save the incoming parameter value

The last line in the constructor saves the incoming value in the instance variable named data , overwriting the default value of zero that it finds there.

This statement is more in keeping with the intended usage of a constructorthan the first two statements. The primary purpose of a constructor is to assist in the initialization of the state of an object , which depends on the values stored in its variables.

Override the abstract getData method

Listing 3 overrides the abstract getData method declared in the abstract superclass named Prob04 and inherited into the subclass named Prob04MyClass .

Listing 3 . Override the abstract getData method.
public int getData(){//overridden abstract method return data;}//end getData()

Very simple behavior

Although the overridden version of the method simply returns a copy of the value stored in the private instance variable named data , it is concrete and can be executed. We will see later that it is called in the main method of the driver class named Prob04 in Listing 1 .

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