0
0
Javaprogramming~10 mins

Why polymorphism is needed in Java - Test Your Understanding

Choose your learning style9 modes available
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.