Polymorphism is the concept that an object can take the form of many different types. We touched upon polymorphism a little with our previous Pet, Dog and Cat classes. Specifically, how both cats and dogs can use properties and methods from their parent class since they are also pets:
Method overloading is when you create multiple methods with the same name but different parameters. This is useful for when you want to have optional parameters.
In this example of method overloading, we have the same method name, getAreaOfRectangle() twice, but depending on which one you use, the method will function slightly different.
Method Overriding
Method overriding is when you define a method with the same name and parameters as one used by the parent class. When you do this, calling that method will result in the child's class method being run and not the parent's.
We defined printInfo() in both the child and parent class. Since they both were named the same with the same parameters, Java automatically used the children's implementation, which includes a different string at the start. Overriding methods is super useful for when you want to have specialized behavior in a child object when compared to the more general parent object.