Bird
Raised Fist0
Javaprogramming~10 mins

Why polymorphism is needed in Java - Test Your Understanding

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a method that all shapes can use.

Java
public abstract class Shape {
    public abstract void [1]();
}
Drag options to blanks, or click blank then click option'
Adraw
Bpaint
Cshow
Ddisplay
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Choosing a method name that is not descriptive of the action.
Using a method name that is not consistent with common naming.
2fill in blank
medium

Complete the code to override the draw method in Circle class.

Java
public class Circle extends Shape {
    @Override
    public void [1]() {
        System.out.println("Drawing a circle");
    }
}
Drag options to blanks, or click blank then click option'
Apaint
Bdraw
Cshow
Ddisplay
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a different method name than the abstract method.
Forgetting to use @Override annotation.
3fill in blank
hard

Fix the error in the code to call the draw method polymorphically.

Java
Shape shape = new Circle();
shape.[1]();
Drag options to blanks, or click blank then click option'
Adraw
Bdisplay
Cshow
Dpaint
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Calling a method not declared in Shape.
Trying to call a method that does not exist.
4fill in blank
hard

Fill both blanks to create a list of shapes and call draw on each.

Java
List<[1]> shapes = new ArrayList<>();
shapes.add(new Circle());
for ([2] shape : shapes) {
    shape.draw();
}
Drag options to blanks, or click blank then click option'
AShape
BCircle
CObject
DDrawable
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using subclass type for the list which limits polymorphism.
Using unrelated types that do not have draw method.
5fill in blank
hard

Fill all four blanks to demonstrate polymorphism with different shapes.

Java
Shape [1] = new Circle();
Shape [2] = new Rectangle();
[3].draw();
[4].draw();
Drag options to blanks, or click blank then click option'
AcircleShape
BrectShape
Cshape1
Dshape2
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using the same variable name for both shapes.
Calling draw on a variable not declared.

Practice

(1/5)
1. Why is polymorphism needed in Java programming?
easy
A. To allow one interface to be used for different data types
B. To increase the speed of the program execution
C. To reduce the size of the compiled code
D. To make the program run only on specific devices

Solution

  1. Step 1: Understand polymorphism concept

    Polymorphism means one name can represent many forms, especially methods or objects.
  2. Step 2: Identify its purpose in Java

    It allows writing code that works with different types through a common interface.
  3. Final Answer:

    To allow one interface to be used for different data types -> Option A
  4. Quick Check:

    Polymorphism = One interface, many types [OK]
Hint: Polymorphism means one name, many forms [OK]
Common Mistakes:
  • Thinking polymorphism speeds up execution
  • Confusing polymorphism with code size reduction
  • Believing it limits device compatibility
2. Which of the following Java code snippets correctly demonstrates polymorphism?
easy
A. Dog d = new Animal(); d.sound();
B. Dog d = new Dog(); d.bark();
C. Animal a = new Animal(); a.sound();
D. Animal a = new Dog(); a.sound();

Solution

  1. Step 1: Check object assignment compatibility

    Polymorphism allows a superclass reference to point to a subclass object, like Animal a = new Dog();
  2. Step 2: Verify method call correctness

    Calling a method on the superclass reference that is overridden in subclass shows polymorphism.
  3. Final Answer:

    Animal a = new Dog(); a.sound(); -> Option D
  4. Quick Check:

    Superclass ref to subclass object = polymorphism [OK]
Hint: Superclass reference can hold subclass object [OK]
Common Mistakes:
  • Assigning superclass object to subclass reference
  • Using subclass-specific methods on superclass reference
  • Ignoring method overriding in polymorphism
3. What will be the output of the following Java code?
class Animal {
  void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
  void sound() { System.out.println("Dog barks"); }
}
public class Test {
  public static void main(String[] args) {
    Animal a = new Dog();
    a.sound();
  }
}
medium
A. Animal sound
B. Compilation error
C. Dog barks
D. Runtime error

Solution

  1. Step 1: Identify polymorphic call

    Variable 'a' is of type Animal but refers to a Dog object.
  2. Step 2: Determine method execution

    At runtime, Dog's overridden sound() method is called, printing "Dog barks".
  3. Final Answer:

    Dog barks -> Option C
  4. Quick Check:

    Overridden method runs based on object type [OK]
Hint: Method called depends on actual object, not reference type [OK]
Common Mistakes:
  • Expecting superclass method output
  • Confusing compile-time and runtime method binding
  • Thinking code causes errors
4. Identify the error in this Java code related to polymorphism:
class Animal {
  void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
  void bark() { System.out.println("Dog barks"); }
}
public class Test {
  public static void main(String[] args) {
    Animal a = new Dog();
    a.bark();
  }
}
medium
A. Cannot assign Dog object to Animal reference
B. Method bark() is not found in Animal class
C. Missing override annotation
D. No error, code runs fine

Solution

  1. Step 1: Check reference type methods

    Variable 'a' is of type Animal, which does not have method bark().
  2. Step 2: Understand method call rules

    At compile time, only methods in Animal class are accessible via 'a'. bark() is undefined there.
  3. Final Answer:

    Method bark() is not found in Animal class -> Option B
  4. Quick Check:

    Reference type limits accessible methods [OK]
Hint: Reference type decides accessible methods [OK]
Common Mistakes:
  • Thinking subclass methods are always accessible
  • Ignoring compile-time method checking
  • Assuming override annotation is mandatory
5. How does polymorphism help in maintaining and extending Java programs?
hard
A. By allowing new classes to be added with minimal changes to existing code
B. By forcing all classes to have the same methods with identical code
C. By preventing any changes once the program is written
D. By making the program run faster on all machines

Solution

  1. Step 1: Understand polymorphism's role in code flexibility

    Polymorphism allows new subclasses to be created that fit existing interfaces.
  2. Step 2: See how it affects maintenance and extension

    Existing code can use new classes without modification, making programs easier to grow and maintain.
  3. Final Answer:

    By allowing new classes to be added with minimal changes to existing code -> Option A
  4. Quick Check:

    Polymorphism enables easy extension [OK]
Hint: Polymorphism means add new classes without changing old code [OK]
Common Mistakes:
  • Thinking polymorphism forces identical method code
  • Believing it stops program changes
  • Assuming it improves speed directly