Team 8

Bailey Say, Andrew Meng, Nicholas Mounier, Rohan Juneja, Aarav Arora

Hacks 1-3

public class Bottle {
    private String matterState;
    private String material;

    // Parent constructor
    public Bottle(String matterState, String material) {
        this.matterState = matterState;
        this.material = material;
    }

    // Two different methods called sayHello, but with different constructors (or method signatures)
    public void sayHello() {
        System.out.println("I am a bottle");
    }

    public void sayHello(String name) {
        System.out.println("I am a bottle called " + name);
    }
}

public class WaterBottle extends Bottle {
    private String liquid;
    
    // Child constructor; note how it uses super() to automatically initialize some of the attributes
    public WaterBottle(String matterState, String material, String liquid) {
        super(matterState, material);
        this.liquid = liquid;
    }

    // Overriding a method
    @Override
    public void sayHello() {
        System.out.println("I am a water bottle");
    }

    // Overriding a method
    @Override
    public void sayHello(String name) {
        System.out.println("I am a water bottle called " + name);
    }
}

Notes

  • Classes are allowed to inherit attributes and methods from other classes using the "extends" keyword
    • Class inheriting is the child class, class being inherited from is the parent class
  • super() can be used to call methods (and constructors) from the parent class inside the child class
    • Subclass constructor can use super() to quickly set attributes originally from the parent class
  • Polymorphism
    • Child class can be treated as an instance of its parent class (referencing superclass of object)
      • Child won't be able to access its own methods this way
    • Methods can be overriden with new definitions
      • @Override keyword
    • Methods can be overloaded with different function signatures
      • Same name, different parameters
  • Late binding - Methods are searched for by the compiler during runtime, not during the compilation of the code. Think about overriding the method
  • Abstract class + methods
    • Abstraction
    • Cannot create an object from it directly, must be created using a child class inheriting from the abstract class
    • Abstract methods originally only have their signature created and must be fully established in the child class definition
  • Standard methods - methods inherited from the original Object class
    • toString() - will usually print out the memory address of an object, can be overriden to print more specific information about the object in question
    • equals() - checks if two objects are equal using "==" (which checks if the two memory addresses point to the same object); can also be overriden for more specific terms of being equal
    • hashCode() - generates a unique numeric ID for each object, which can be used to check if they're equal to one another; can also be overriden for more specific terms