WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Core Java · Intermediate · question 28 of 100

What is polymorphism in Java? How is it achieved?

📕 Buy this interview preparation book: 100 Core Java questions & answers — PDF + EPUB for $5

Polymorphism is the ability of an object to take on many forms. In Java, polymorphism is achieved through two mechanisms: method overloading and method overriding.

Method overloading is a feature in Java that allows a class to have multiple methods with the same name but different parameters. When a method is called, the Java compiler determines which method to call based on the number and types of arguments passed to it. Here’s an example:

public class MyClass {
    public void print(String message) {
        System.out.println(message);
    }
    
    public void print(int number) {
        System.out.println(number);
    }

}

MyClass obj = new MyClass();
obj.print("Hello"); // calls the first print() method
obj.print(42); // calls the second print() method

In this example, the MyClass class has two methods with the same name (print()) but different parameters. When we call the print() method with a string argument, the first print() method is called, and when we call the print() method with an integer argument, the second print() method is called.

Method overriding, on the other hand, is a feature in Java that allows a subclass to provide a specific implementation of a method that is already provided by its superclass. When a method is called on an object of the subclass, the Java virtual machine determines which implementation of the method to call based on the actual type of the object. Here’s an example:

public class Animal {
    public void makeSound() {
    System.out.println("The animal makes a sound");
    }
}

public class Cat extends Animal {
    public void makeSound() {
        System.out.println("Meow");
    }
}

Animal animal = new Animal();
animal.makeSound(); // calls the makeSound() method of the Animal class
Cat cat = new Cat();
cat.makeSound(); // calls the makeSound() method of the Cat class
Animal animal2 = new Cat();
animal2.makeSound(); // calls the makeSound() method of the Cat class

In this example, the Animal class has a makeSound() method that prints a generic message. The Cat class extends the Animal class and overrides the makeSound() method with a specific implementation that prints "Meow". When we call the makeSound() method on an object of the Animal class, the makeSound() method of the Animal class is called. When we call the makeSound() method on an object of the Cat class, the makeSound() method of the Cat class is called. When we call the makeSound() method on an object of the Cat class stored in an Animal reference variable, the makeSound() method of the Cat class is still called, because the actual type of the object is Cat.

In summary, polymorphism is the ability of an object to take on many forms, and in Java, it is achieved through two mechanisms: method overloading and method overriding. Method overloading allows a class to have multiple methods with the same name but different parameters, and method overriding allows a subclass to provide a specific implementation of a method that is already provided by its superclass.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Core Java interview — then scores it.
📞 Practice Core Java — free 15 min
📕 Buy this interview preparation book: 100 Core Java questions & answers — PDF + EPUB for $5

All 100 Core Java questions · All topics