Java 17 Recipes


-12. Extending the Functionality of a Class


Download 3.2 Mb.
Pdf ko'rish
bet108/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   104   105   106   107   108   109   110   111   ...   245
Bog'liq
Java 17 Recipes

5-12. Extending the Functionality of a Class
 Problem
One of your applications contains a class that you want to use as a base for another class. 
You want your new class to contain the same functionality as this base class but also 
include additional functionality.
 Solution
Extend the functionality of the base class by using the extends keyword followed 
by the name of the class that you want to extend. The following example shows two 
classes. The first class, named HockeyStick, represents a hockey stick object. It is 
extended by the second class named WoodenStick. By doing so, the WoodenStick 
class inherits all the properties and functionality within the HockeyStick class, except 
private variables and those with the default access level. The WoodenStick class 
becomes a subclass of HockeyStick. First, let’s take a look at the HockeyStick class, 
which contains the basic properties of a standard hockey stick.
public class HockeyStick {
private int length;
private boolean curved;
private String material;
public HockeyStick(int length, boolean curved, String material){
this.length = length;
this.curved = curved;
this.material = material;
}
/**
* @return the length
*/
public int getLength() {
return length;
}
Chapter 5 ObjeCt-Oriented java


195
/**
* @param length the length to set
*/
public void setLength(int length) {
this.length = length;
}
/**
* @return the curved
*/
public boolean isCurved() {
return curved;
}
/**
* @param curved the curved to set
*/
public void setCurved(boolean curved) {
this.curved = curved;
}
/**
* @return the material
*/
public String getMaterial() {
return material;
}
/**
* @param material the material to set
*/
public void setMaterial(String material) {
this.material = material;
}
}
Next, look at the subclass of HockeyStick, a class named WoodenStick.
public class WoodenStick extends HockeyStick {
private static final String material = "WOOD";
Chapter 5 ObjeCt-Oriented java


196
private int lie;
private int flex;
public WoodenStick(int length, boolean isCurved){
super(length, isCurved, material);
}
public WoodenStick(int length, boolean isCurved, int lie, int flex){
super(length, isCurved, material);
this.lie = lie;
this.flex = flex;
}
/**
* @return the lie
*/
public int getLie() {
return lie;
}
/**
* @param lie the lie to set
*/
public void setLie(int lie) {
this.lie = lie;
}
/**
* @return the flex
*/
public int getFlex() {
return flex;
}
/**
* @param flex the flex to set
*/
public void setFlex(int flex) {
this.flex = flex;
}
}
Chapter 5 ObjeCt-Oriented java


197
Note in this example, we assume that there may be more than one type of 
hockeyStick. in this case, we extend hockeyStick to create a WoodenStick, but 
we may also extend hockeyStick to create other types of hockeyStick, such as 
aluminumStick or GraphiteStick.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   104   105   106   107   108   109   110   111   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling