- Hands-On Design Patterns with Java
- Dr. Edward Lavieri
- 1065字
- 2021-06-24 14:58:01
Learning the complete OOP class
Throughout this chapter, we made use of a Bicycle class. As illustrated in the following, we created a hierarchy to demonstrate inheritance as a key feature of OOP. All objects inherit from the Object class in Java. Our hierarchy has Bicycle inheriting from TwoWheeled, which inherits from Vehicle, which inherits from Object:
For our Bicycle class to work with the intended hierarchy, we created classes for Vehicle and TwoWheeled. Here are those classes:
public class Vehicle {
}
public class TwoWheeled extends Vehicle {
}
The Vehicle and TwoWheeled classes do not offer anything to the Bicycle class. Normally, each would have attributes and behaviors associated with them.
The completed Bicycle class, as refined throughout this chapter, is provided in the following sections. This first section has the class definition and the four instance variables:
public class Bicycle extends TwoWheeled {
// instance variable declarations
private int gears = 0;
private double cost = 0.0;
private double weight = 0.0;
private String color = "";
The next section of the Bicycle class contains the constructors. The first constructor does not take any parameters and is the default constructor. The second constructor accepts a String parameter. The third constructor accepts a single int parameter. The final constructor accepts four parameters:
// constructor - default
Bicycle() {
}
// constructor - String parameter
Bicycle(String aColor) {
this.color = aColor;
}
// constructor - int parameter
Bicycle(int nbrOfGears) {
this.gears = nbrOfGears;
}
// constructor - int, double, double, String parameters
Bicycle(int nbrOfGears, double theCost, double theWeight, String aColor) {
this.gears = nbrOfGears;
this.cost = theCost;
this.weight = theWeight;
this.color = aColor;
}
After the overloaded constructor methods, we have two methods to output data. The outputData() method is overloaded so that it can be used without any parameters or with a single String parameter:
// method to output Bicycle's information
public void outputData() {
System.out.println("\nBicycle Details:");
System.out.println("Gears : " + this.gears);
System.out.println("Cost : " + this.cost);
System.out.println("Weight : " + this.weight + " lbs");
System.out.println("Color : " + this.color);
}
// method to output Bicycle's information - overloaded
// - method call chaining enabled
public Bicycle outputData(String bikeText) {
System.out.println("\nBicycle " + bikeText + " Details:");
System.out.println("Gears : " + this.gears);
System.out.println("Cost : " + this.cost);
System.out.println("Weight : " + this.weight + " lbs");
System.out.println("Color : " + this.color);
return this;
}
The next section of our Bicycle class contains the four Accessor methods, one for each of the instance variables:
// Accessors (Getters)
public int getGears() {
return this.gears;
}
public double getCost() {
return this.cost;
}
public double getWeight() {
return this.weight;
}
public String getColor() {
return this.color;
}
The final section of our Bicycle class contains the four Mutator methods, one for each of the instance variables:
// Mutators (Setters) - method call chaining enabled
public Bicycle setGears(int nbr) {
this.gears = nbr;
return this;
}
public Bicycle setCost(double amt) {
this.cost = amt;
return this;
}
public Bicycle setWeight(double lbs) {
this.weight = lbs;
return this;
}
public Bicycle setColor(String theColor) {
this.color = theColor;
return this;
}
}
We also made several changes to our Driver class. That class is provided here over several successive sections. The code includes in-code comments to organize and identify each code block:
- This first code block defines the class, includes the main() method definition, creates a myBike instance of Bicycle, and provides example calls to mutators. Then, a sample output statement is provided that makes a call to the getColor() accessor method:
public class Driver {
public static void main(String[] args) {
// Example calls to mutators
Bicycle myBike = new Bicycle();
myBike.setGears(24);
myBike.setCost(319.99);
myBike.setWeight(13.5);
myBike.setColor("Purple");
System.out.println("\nmyBike's color is " + myBike.getColor());
- The second block of code provides example calls to the overloaded constructor. There are four instances of the Bicycle object created, each using a different overloaded constructor:
// Example of calls to overloaded constructor
Bicycle myBike1 = new Bicycle();
Bicycle myBike2 = new Bicycle("Brown");
Bicycle myBike3 = new Bicycle(22);
Bicycle myBike4 = new Bicycle(22, 319.99, 13.5, "White");
- Our third code block makes four calls to the overloaded outputData() method:
myBike1.outputData("Nbr 1");
myBike2.outputData("Nbr 2");
myBike3.outputData("Nbr 3");
myBike4.outputData("Nbr 4");
- The fourth code block provides an example of method call chaining:
// Example using method call chaining
Bicycle myBike5 = new Bicycle(24, 418.50, 17.2, "Green");
myBike5.setColor("Peach").setGears(32).outputData("Number 5");
- The fifth code bock provides "IS A" Checks for the Bicycle class. We start by creating an instance of Bicycle and then run our checks against the newly created myBike6 object:
// "IS A" Checks
System.out.println("\n\"IS A\" CHECKS");
// focus on myBike6
Bicycle myBike6 = new Bicycle();
if (myBike6 instanceof Bicycle)
System.out.println("myBike6 Instance of Bicycle: True");
else
System.out.println("myBike6 Instance of Bicycle: False");
if (myBike6 instanceof TwoWheeled)
System.out.println("myBike6 Instance of TwoWheeled: True");
else
System.out.println("myBike6 Instance of TwoWheeled: False");
if (myBike6 instanceof Vehicle)
System.out.println("myBike6 Instance of Vehicle: True");
else
System.out.println("myBike6 Instance of Vehicle: False");
if (myBike6 instanceof Object)
System.out.println("myBike6 Instance of Object: True");
else
System.out.println("myBike6 Instance of Object: False");
- The sixth code bock provides the "IS A" Checks for the TwoWheeled class. We start by creating an instance of TwoWheeled and then running our checks against the newly created myTwoWheeled object:
// focus on TwoWheeled
TwoWheeled myTwoWheeled = new TwoWheeled();
if (myTwoWheeled instanceof Bicycle)
System.out.println("\nmyTwoWheeled Instance of Bicycle: True");
else
System.out.println("\nmyTwoWheeled Instance of Bicycle: False");
if (myTwoWheeled instanceof TwoWheeled)
System.out.println("myTwoWheeled Instance of TwoWheeled: True");
else
System.out.println("myTwoWheeled Instance of TwoWheeled: False");
if (myTwoWheeled instanceof Vehicle)
System.out.println("myTwoWheeled Instance of Vehicle: True");
else
System.out.println("myTwoWheeled Instance of Vehicle: False");
if (myTwoWheeled instanceof Object)
System.out.println("myTwoWheeled Instance of Object: True");
else
System.out.println("myTwoWheeled Instance of Object: False");
- The seventh and final code bock provides the "IS A" Checks for the Vehicle class. We start by creating an instance of Vehicle and then running our checks against the newly created myVehicle object:
// focus on Vehicle
Vehicle myVehicle = new Vehicle();
if (myVehicle instanceof Bicycle)
System.out.println("\nmyVehicle Instance of Bicycle: True");
else
System.out.println("\nmyVehicle Instance of Bicycle: False");
if (myVehicle instanceof TwoWheeled)
System.out.println("myVehicle Instance of TwoWheeled: True");
else
System.out.println("myVehicle Instance of TwoWheeled: False");
if (myVehicle instanceof Vehicle)
System.out.println("myVehicle Instance of Vehicle: True");
else
System.out.println("myVehicle Instance of Vehicle: False");
if (myVehicle instanceof Object)
System.out.println("myVehicle Instance of Object: True");
else
System.out.println("myVehicle Instance of Object: False");
}
}
The complete output of the Driver class is provided as follows:
In this section, we demonstrated OOP using a complete OOP Bicycle application.