Notes

  • Superclass: A parent class in which it's attributes and methods are inherited
  • Super keyword: Adds the constructor of the parent to the child
  • Subclass: A class that inherits attributes and methods
  • Override: Allows a subclass to override and redefine a parent class
  • Polymorphism: Reusing a function. This can be through method overloading or through overriding a method
  • Runtime polymorphism: This is if the function is reused while the program is running. An example of this would be method overloading
  • Method overloading: Passing different numbers of parameters through a function to have it run differently each time. An example of this would be no arg / multi arg constructors for classes
  • Static Polymorphism: Polymorphism that only exists in one class
  • Early Binding: Defines a data type when it is instantiated
  • Late Binding: Defines a data type later after it is instantiates. This could be used if theres an object that you know will exist, but you dont know what child it would be. (Animal timmy = new Chicken();)

Hack 1 & Hack 2

have a super class w no and multi arg constructor, then extend it with another attribute

have an override funciton

abstract class Piece {

    // Multiple attributes
    String color;
    int row, file;
    ArrayList<ArrayList<Integer>> legalMoves = new ArrayList<ArrayList<Integer>>();

    // Multi arg constructor
    public Piece(String color, int row, int file) {
        this.color = color;
        this.row = row;
        this.file = file;
    }

    // No arg constructor
    public Piece() {
        this.color = "white";
        this.row = 0;
        this.file = 0
    }

    // function to be overridden later
    protected void takePiece(ArrayList<Integer> move) {
        for (ArrayList<Integer> legalMove : legalMoves) {
            if (legalMove.equal(move)) {
                // take the piece implementation not shown
            }
        }
    }
}
public class Pawn extends Piece {

    // New attribute
    boolean cool;
    
    // New constructor
    public Pawn(String color, int row, int file, boolean cool) {
        super(color, row, file);
        this.cool = cool;
    }

    private void addLegalSquares() {
        if (this.color == "white") {
            ArrayList<Integer> newLegalSquares = new ArrayList<Integer>();
            newLegalSquares.add(row);
            newLegalSquares.add(file+1);
            legalSquares.add(newLegalSquares);
        } else if (this.color == "black") {
            ArrayList<Integer> newLegalSquares = new ArrayList<Integer>();
            newLegalSquares.add(row);
            newLegalSquares.add(file-1);
            legalSquares.add(newLegalSquares);
        }
    }

    // Hack 2 with an overriden function
    @Override
    public void takePiece(ArrayList<Integer> move) {
        for (ArrayList<Integer> legalMove : legalMoves) {
            if (legalMove.equal(move)) {
                // take the piece implementation not shown
            }
        }

        // implement something for enpassant
    }
}

Hack 3

Implement method overloading

public class Math {
    public static int sum(int a) {
        return a;
    }

    public static int sum(int a, int b) {
        return a + b;
    }

    public static int sum(int a, int b, int c) {
        return a + b + c;
    }

    public static int sum(int a, int b, int c, int d) {
        return a + b + c + d;
    }

    public static void main(String[] args) {
        System.out.println(sum(1));
        System.out.println(sum(1,2));
        System.out.println(sum(1,2,3));
        System.out.println(sum(1,2,3,4));
    }
}

Math.main(null);
1
3
6
10