Beruflich Dokumente
Kultur Dokumente
Every Warrior needs to have generateStats() and victory() defined for them.
Shouldn’t they be defined in the base class Warrior as well?
Warrior
data fields: name, strength, IQ
methods: toString(), getStrength()
getIQ()
Every Warrior needs to have generateStats() and victory() defined for them.
Shouldn’t they be defined in the base class Warrior as well? YES
How do you generateStats for a Warrior if you don’t know what clique they are?
Warrior
data fields: name, strength, IQ
methods: toString(), getStrength()
getIQ()
Every Warrior needs to have generateStats() and victory() defined for them.
Shouldn’t they be defined in the base class Warrior as well? YES
How do you generateStats for a Warrior if you don’t know what clique they are?
I don’t know. Make them abstract for Warrior.
The Nerd subclass
public class Nerd extends Warrior
{
public Nerd(String x) the constructor for each subclass need
{ only be sent the name, and
super(x, “nerd”); pass that name and the clique on to the
} constructor for the base class Warrior
public void generateStats()
{
strength = (int)(Math.random()*61) + 20; //20-80
IQ = (int)(Math.random()*61) + 120; //120-180
}
public void victory()
{
System.out.println(name + “ dispatches their opponent with a combo Jedi mind trick
and Vulcan neck pinch!”);
}
}
public, private and protected
• For the Nerd subclass, we inherit the name,
strength and IQ data fields, but can not access
them if they are declared as private in Warrior.
so, make them protected in Warrior
public – can be accessed or modified from any
program in the same folder or project.
private – can only be accessed or modified from
within the class it is defined.
protected – can only be accessed or modified from
within the class it is defined, or any subclass of that
class (protected within the family).
Abstract methods and classes
• Abstract method: a method header with no code body.
public abstract void generateStats();
All we know about it is: the return type (if any), the name,
and any needed arguments.
• Abstract class: a class definition that contains at least one
abstract method.
– The first concrete subclass of an abstract class must define
the abstract method concretely (give it a code body).
– You can not create an instance of an abstract object
– The abstract super class contains all of the code that is
common between the concrete base classes.
abstract Warrior
data fields: name, strength, IQ
methods: toString(), getStrength()
getIQ() , getName(),
abstract void generateStats();
abstract void victory();