StackTips

Java Polymorphism - Method Overloading, Method Overriding

stacktips avtar

Written by

Editorial,  8 min read,  2 views, updated on Dec. 21, 2023

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:

public 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();
    }
}
Bark!
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:

public 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;
    }
}
9
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:

public 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();
    }
}
Name: 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.

Beginner's Guide to Java Collections

This course covers the fundamentals of java collections framework covering different data structures in Java to store, group, and retrieve objects.

>> CHECK OUT THE COURSE

Continue reading..