Table of Contents
Polymorphism
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:
JAVApublic class Main {
public static class Pet {
String name;
int weight;
Pet(String name, int weight) {
this.name = name;
this.weight = weight;
}
void printInfo() {
System.out.println("Name: " + name + " Weight: " + weight + " pounds.");
}
}
public static class Cat extends Pet {
Cat(String name, int weight) {
super(name, weight);
}
void meow() {
System.out.println("Meow!");
}
}
public static class Dog extends Pet {
Dog(String name, int weight) {
super(name, weight);
}
void bark() {
System.out.println("Bark!");
}
}
public static void main(String[] args) {
Dog dog = new Dog("Scooby", 4);
dog.bark();
dog.printInfo();
Cat cat = new Cat("Tinkerbell", 9);
cat.meow();
cat.printInfo();
}
}
BASHBark!
Name: Scooby Weight: 4 pounds.
Meow!
Name: Tinkerbell Weight: 9 pounds.
Method Overloading
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.
Here's an example of method overloading:
JAVApublic class Main {
public static void main(String[] args) {
int squareSideLength = 3;
int squareArea = getAreaOfRectangle(squareSideLength);
System.out.println(squareArea);
int rectangleWidthLength = 5;
int rectangleHeightLength = 6;
int rectangleArea = getAreaOfRectangle(rectangleWidthLength, rectangleHeightLength);
System.out.println(rectangleArea);
}
public static int getAreaOfRectangle(int width, int height) {
return width * height;
}
public static int getAreaOfRectangle(int length) {
return length * length;
}
}
BASH9
30
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.
Let's take a look at how this looks in practice:
JAVApublic class Main {
public static class Pet {
String name;
int weight;
Pet(String name, int weight) {
this.name = name;
this.weight = weight;
}
void printInfo() {
System.out.println("Name: " + name + " Weight: " + weight + " pounds.");
}
}
public static class Cat extends Pet {
Cat(String name, int weight) {
super(name, weight);
}
void printInfo() {
System.out.println("Cat Name: " + name + " Cat Weight: " + weight + " pounds.");
}
}
public static void main(String[] args) {
Pet pet = new Pet("Scooby", 4);
pet.printInfo();
Cat cat = new Cat("Tinkerbell", 9);
cat.printInfo();
}
}
BASHName: Scooby Weight: 4 pounds.
Cat Name: Tinkerbell Cat Weight: 9 pounds.
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.
- Getting Started with TypeScript
- Getting Started with Solid
- How to Serve Static Files with Nginx and Docker
- How to deploy a .NET app using Docker
- How to deploy a MySQL Server using Docker
- How to deploy an Express app using Docker
- How to deploy a Node app using Docker
- Getting Started with Sass
- Creating a Twitter bot with Node.js
- Using Push.js to Display Web Browser Notifications
- Getting Started with Vuex: Managing State in Vue
- Using Axios to Pull Data from a REST API