Inheritance
Inheritance
Inheritance is
Passing down traits or characteristics from a parent to their child like
skin, hair color
Inheritance enable polymorphism
Checking account
Saving account
Certificate of deposit
They all share similar information like the account number and
the balance in them
But they also have different attributes
class BankAccount{
String acctNumber;
double balance;
parent
}
While still having the flexibility and the good design of separate
account classes
E.g., A class extends another class, an object of the subclass can be treated
as an instance of both its own class and its parent class
extends extends
STUDENT TEACHER
double grade int level
int year int yearOfExp
Crossbow
Coins
int power
int amount int weight
int weight
Key
RareItem
int code
int value
int weight
int weight
items
Item
int weight
extends
extends
Coins Crossbow
int amount extends extends int power
RareItem Key
int value int code
Bags
public class Bag{
Item[] list;
int count;
int currentWeight;
Bag(){list = new Item[100];
count =0; currentWeight =0;
}
boolean canAddItem(Item item){…}
void addItem(Item item){…}
}
Bags…
boolean canAddItem(Item item){
if(currentWeight + item.weight >20)
return false;
else
return true;
}
void addItem(Item item){
list[count]= item;
}
Board class
Represents the chessboard by 2D array of Square objects (Square[][] board)
Manage the state of the chessboard, including the pieces on each square
Allow the game to place and move pieces and keep track of which squares are
occupied
Other classes
class Board{
Square [][] board;
Game(){
board = new Piece[8][8];
}…..
}
Position class
Represents the coordinate of a square on the chessboard
class Position{
int row; int column;
Position(int row, int column){
this.row=row; this.column=column; } ….
}
Other classes…
Square class
Represents a single square on the chessboard, which can either be empty or
occupied by a piece
class Square{
Position position;
Piece occupant;
….
}
Pieces class
serves as the base class for all chess pieces, defining common
attributes and behaviors that each piece type (King, Queen,…) shares
Other classes…
E.g., it will be useful to have a
method that checks if a potential
movement of a piece is valid one
validMove() is used to get
valid moves for the piece
…..
isValidMove() to check if a
piece’s move is valid
Other classes…
class Piece{
Position position;
…….
boolean isValidMove(Position newPosition){
if(newPosition.row > 0 && newPosition.column >0 && newPosition<8
&& newPosition.column <8)
return true;
else
return false;
}
}
Other classes…
Since each of the child class inherits from that Piece class, each will
automatically include this method, which means you can call it from any
of those classes directly
E.g.,
Queen queen = new Queen();
Position testPosition = new Position(3,3);
if(queen.isValidMove(testPosition))
System.out.println(“Yes, you can move there”);
else
System.out.println(“Nope, cannot move there”);
Luckily, java not only offers a way to inherit a method from a parent class
but also modify it to build your own custom version of it
Notice how both method declarations are identical, except that the
implementation in the child class has different code customizing the validity check
for the Rook piece
has the same column value as the current position (which means it’s trying to
move up or down )
or has the same row position which means it has moved sideways,
To re-use the parent method in the child class by using the “super”
keyword, followed by a dot and then the method name
super.isValidMove(position);
before you get to check the custom movement, you can check if
super.isValidMove(position) has returned false
If so, then no need to do any more checks and immediately return false
The new implementation for the Rook class will look like….
if(newPosition.column==this.position.column &&
newPosition.row==this.position.row)
return true;
else
return false;
}