Polymorphism

Computer Science · Java

Question 1 of 10

0

    What number does this Java code print? public class Main { public static void main(String[] args) { Employee obj = new Manager(); System.out.println(obj.sound()); } } class Employee { int sound() { return 5; } } class Manager extends Employee { @Override int sound() { return 8; } }

    Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.

    How to type your answer
    • Fractions: 3/4. Mixed numbers: 1 1/2.
    • Decimals with a point or a comma: 0.75 or 0,75.
    • Negative numbers start with a minus: -3/4.
    • Percentages need the % sign: 75%.

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Polymorphism explained

    Polymorphism means "many forms": the same method call can produce different results depending on which object it is called on. It is often achieved through inheritance, where each subclass overrides a method it inherited from its parent to give its own version.

    Suppose a parent class Shape has a method area(). Rectangle and Triangle both inherit from Shape, and each one overrides area() with its own formula. A program can keep a list of shapes and call area() on each one. It does not need a separate if-statement for each shape type, because each object runs its own version of the method.

    Overriding is not the same as overloading. Overriding replaces an inherited method in a subclass. Overloading means one class has several methods with the same name but different parameters. Many students mix these up, so check which one a question is asking about.

    A common mistake is writing the overriding method with a different name or different parameters, so the program quietly calls the parent's version instead.

    Worked example

    A list holds three shapes, and the code below adds up their areas:

    total = 0
    for s in shapes:
        total = total + s.area()
    

    The shapes are Rectangle(3, 4), Triangle(6, 5) and Rectangle(2, 2). Each area() call uses the formula for that object.

    1. Rectangle(3, 4): 3×4=123 \times 4 = 12.
    2. Triangle(6, 5): 12×6×5=15\tfrac{1}{2} \times 6 \times 5 = 15.
    3. Rectangle(2, 2): 2×2=42 \times 2 = 4.
    4. The total is 12+15+4=3112 + 15 + 4 = 31.

    Check: the same call, area(), gave three different formulas, and the loop never needed to know which shape was which.

    What you will practise

    New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.

    Example questions

    • Easy

      What number does this Java code print?

      public class Main { public static void main(String[] args) { Employee obj = new Manager(); System.out.println(obj.sound()); } }

      class Employee { int sound() { return 2; } }

      class Manager extends Employee { @Override int sound() { return 7; } }

    • Medium

      What number does this Java code print?

      public class Main { public static void main(String[] args) { Employee obj = new Manager(); System.out.println(obj.sound()); } }

      class Employee { int sound() { return 5; } }

      class Manager extends Employee { @Override int sound() { return super.sound() + 3; } }

    • Hard

      What number does this Java code print?

      public class Main { static int describe(Plant x) { return x.sound(); }

      static int describe(Tree x) { return x.sound() + 1; }

      public static void main(String[] args) { Tree obj = new Tree(); System.out.println(describe(obj)); } }

      class Plant { int sound() { return 3; } }

      class Tree extends Plant { @Override int sound() { return 6; } }