class ScreenCharacter {...}
class Letter extends ScreenCharacter {...}
class Digits extends ScreenCharacter {...}
The keyword extends is used to distinguish the subclass (child class) and the superclass (parent class). If nothing is said about the superclass, then Java automatically considers it as a subclass of the Object class. So, with the above declarations you have constructed the following class hierarchy.The point is that all instance variables and methods that belong to the
ScreenCharacter class are inherited by its subclasses so that they need not be defined again. For example, if at the higher level the ScreenCharacter class look like
class ScreenCharacter {
String name;
String fontname;
int fontsize;
int x, y;
void draw(Graphics g) {
g.setFont(new Font(fontname, Font.BOLD, fontsize));
g.drawString(name, x, y);
}
}
then the subclasses Letter and Digit do not have to introduce these instance variables and method again. By the way, if you are wondering why we chose the long name ScreenCharacter instead of Character, the reason is that the Java system already has a Character class built in (in the java.lang
The Letter class can be introduced as
class Letter extends ScreenCharacter {
String letterCase;
this.letterCase = letterCase;
}
String getCase() {
return letterCase;
}
letterCase = "lowercase";
name = name.toLowerCase();
}
letterCase = "uppercase";
name = name.toUpperCase();
}
}
toUpperCase and toLowerCase methods in terms of methods with the same name in the String class.
No comments:
Post a Comment