Subclass & Setters

Based off of this Date class

Structure: … SubClass > Constructor > Super > Setter

Subclass:

IncDate is a subclass of the Date class because of the extends keyword.

Constructor:

The obligatory (?) constructor of the IncDate subclass is composed of the newMonth, newDay, newYear variable parameters.

Super:

The super keyword passes the constructor’s parameters of the subclass to the superclass. This exists because java isn’t directly able to inherit constructors.

Setter:

The setter method, also known as the Transformer method, changes the “internal” state of an object. For example, setter methods may change the variable of a object from one value to another.

Also notice that setter methods do not return a value, therefor have the void modifier in the method header.

public class IncDate extends Date {

public IncDate(int newMonth, int newDay, int newYear) {
// Initializes this IncDate with the parameter values
super(newMonth, newDay, newYear);
}

public void increment() {
// Increments this IncDate to represent the next day, i.e.,
// this = (day after this)
// For example if this = 6/30/2003 then this becomes 7/1/2003
// Increment algorithm goes here
}
}

 

 

What's Your Opinion?