Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
29
Dessert Example
SUBCLASSES and SUPERCLASSES
public class Dessert {
The Object class is the root of the Java private String name;
class hierarchy. private int calories;
The Object class declares methods that are public Dessert(String nameOf) {
implemented by all objects. name = nameOf;
}
• public boolean equals(Object obj)
public Dessert(String nameOf, int caloriesOf) {
• public int hashCode() name = nameOf;
calories = caloriesOf;
• protected Object clone() }
• protected void finalize() throws Throwable public int caloriesOf() { return calories; }
}
1
J.30 J.31
Testing Desserts and Cakes
public class Cake extends Dessert {
private String cakeType; public class DessertTest {
private String frostType;
public static void main (String[] args) {
public Cake(String cakeNameOf, int cakeCalOf,
String cakeTypeof, String frostTypeOf) { Dessert general = new Dessert("General",500);
System.out.println(general.nameOf() + ": " +
super(cakeNameOf, cakeCalOf); general.caloriesOf());
cakeType = cakeTypeof;
frostType = frostTypeOf; Cake acake = new Cake("Mike’s Cake",800,
} "German Chocolate","Chocolate");
System.out.println(acake.nameOf() + ": " + acake.cakeTypeOf()
public Cake(String cakeNameOf, String cakeTypeOf) { + " " + acake.caloriesOf() + " " + acake.frostTypeOf());
super(cakeNameOf,0);
cakeType = cakeTypeOf; Cake bcake = new Cake("Darrell’s Cake","Carrot");
frostType = "none"; System.out.println(bcake.nameOf() + ": " + bcake.cakeTypeOf()
} + " " + bcake.caloriesOf() + " " + bcake.frostTypeOf());
General: 500
}
Mike’s Cake: German Chocolate 800 Chocolate
Darrell’s Cake: Carrot 0 none
2
Protected Access Solves This Problem J.33
J.32
Can a private field of the parent class public class Dessert {
be changed by a subclass? private String name;
protected int calories;
3
J.34 J.35
What protected Really Means
Overloading vs. Overriding
Dessert
protected int calories; iOverloading a method means providing
more than one method with the same name,
but different signatures.
Cake Scone Pudding
iOverriding a method means replacing the
superclass’s implementation of a method
WhippedCreamCake with one of your own, with an identical
signature.
Each class that extends Dessert inherits calories.
Code in the Cake class can access calories only Public class Cake extends Dessert {
i
through a reference to a type that is a Cake or i
a subclass of Cake. i
public String NameOf() {
Code in the Cake class cannot access the calories return "Cake " + name;
field of a Scone or a Pudding or a generic Dessert.
}
Note: protected static fields can be accessed in
any extended class.
4
J.37
J.36
Fields can be hidden, but not overridden
More Terminology
class SuperShow {
public String str = "SuperStr";
5
J.38 J.39
INTERFACES
Interface Attributed {
void add(String attrName, Attr newAttr);
Attr find(String attrName);
Attr remove(String attrName);
}
In what kinds of applications are abstract
classes and methods useful?
6
J.41
J.40
7
J.42
Name Conflicts