You are on page 1of 17

Game Characters

Kobes  class  
Wing-­‐Back  
class  
Ghoul  class  
Angry  Blue  
Spike  class  

Atomic  Ghoul  
Chopo  class   class  
Characters in a game

ABSpike  class  

Ghoul  class  
Inherited methods
out.println(ScaryHarry.getLife());

if (ScaryHarry.isCaptured()){...

These methods are inherited from


the super class, ABSpike
Constructors
ABSpike class Ghoul class
•  color:  input  parameter   •  color:  input  parameter  
•  captured:  will  be  set  to   •  captured:  will  be  set  to  
default  of  false   default  of  false  
•  life:  set  default  value  to  10   •  life:  set  default  value  to  15  
•  posiCon:  input  parameter   •  posiCon:  input  parameter  
Constructors
ABSpike class Ghoul class
•  color:  input  parameter   •  call  constructor  from  
•  captured:  will  be  set  to   Character  
default  of  false   •  life:  set  default  value  to    
•  life:  set  default  value  to  10      life  +  5  
•  posiCon:  input  parameter  
Ghoul class Constructors
•  call  constructor  from  
super(team, starting position);  
ABSpike  
•  life:  set  default  value  to    
setLife(5);  
   life  +  5  

private void setLife(int change){


life = life + change;
}  
setLife(int) in ABSpike super class

protected void setLife(int change){


life = life + change;
}  

! V isible to the class it is defined in


! V isible to all subclasses
Ghoul class
public class Ghoul extends ABSpike{

public Ghoul(char team, int[] startingPos){


super(team, startingPos);
setLife(5); //life set to default + 5
}

public boolean move (int x, int y){


if(1 == Math.abs(getX() - x) && 1 == Math.abs(getY() - y)){
return(super.move(x, y));
}
return(false);
}
}
Constructors in a subclass
Ghoul
ABSpike
color
Jim   captured
life
position

addi$onal   fields  
and  methods,  
overridden  
methods  
Constructors in a subclass
•  Call  super  constructor  first  
•  If  no  call  to  a  super  class  constructor  is  made  
–  an  implicit  call  to  default  constructor  in  super  class  
is  made  
–  if  there  is  no  default  constructor  in  the  super  class,  
another  constructor  must  explicitly  be  called  
Private methods and fields
•  Are  not  accessible  outside  the  class  where  
they  are  defined  or  declared  
•  Are  not  accessible  to  subclasses,  even  though  
they  are  inherited  
•  Subclasses  must  use  public  or  protected  
methods  to  access  their  own  fields  
The relationship between a super class and its subclass(es)
Class BaseOne
private field x Class  SubTwo  can  
protected method setX() • access  field  y  directly  
private method compareX()
public method updateX() • call  setY()  
public method mutateX() • call  setX()  

Class  SubTwo  can  not  


Class SubTwo extends BaseOne • access  field  x  directly,    
private field y    must  use  getX()  
private method setY()
public method updateX()
• call  the  compareX()  
public method mutateX(int) method  
The relationship between a super class and its subclass(es)
Class BaseOne a  client  program...  
private field x
protected method setX() BaseOne user3 = new BaseOne();
SubTwo user17 = new SubTwo():
private method compareX()
public method updateX() for (int i:= 0; i<k; i++){
public method mutateX() user3.updateX();
}

user17.mutateX(8);
Class SubTwo extends BaseOne
user17.y = 874;
private field y
user3.compareX(user17);
private method setY()
public method updateX()
public method mutateX(int)
The relationship between a super class and its subclass(es)
Class BaseOne
private field x
protected method setX()
private method compareX()
public method updateX()
public method mutateX()

Class SubTwo extends BaseOne


private field y
private method setY()
public method updateX()
public method mutateX(int)
The relationship
updateX()between a super
is overridden class
in the and itsSubTwo
subclass subclass(es)
Class BaseOne public void process(){

public method updateX() BaseOne object1 = new BaseOne();

SubTwo object2 = new SubTwo();

...
object1.updateX();
object2.updateX();
Class SubTwo extends BaseOne ...

public method updateX()


The relationship between a super class and its subclass(es)
Class BaseOne
private field x
protected method setX()
private method compareX()
public method updateX()
public method mutateX()

Class SubTwo extends BaseOne


private field y
private method setY()
public method updateX()
public method mutateX(int)
ThemutateX()
relationshipisbetween a super
overloaded class
in the and its
subclass subclass(es)
SubTwo

Class BaseOne public void process(){

public method mutateX() BaseOne object1 = new BaseOne;

SubTwo object2 = new SubTwo();

...
object2.mutateX();
object2.mutateX(73);
Class SubTwo extends BaseOne ...

public method mutateX(int)

You might also like