Inheritance

Computer Science · Java

Question 1 of 10

0

    What number does this Java code print? public class Main { public static void main(String[] args) { System.out.println(new Dog().value()); } } class Animal { int value() { return 4; } } class Dog extends Animal { }

    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.

    Inheritance explained

    Inheritance lets one class, called a subclass, reuse the attributes and methods of another class, called its superclass or parent. The subclass gets the parent's accessible attributes and methods, and it can then add new members or replace some of the parent's methods.

    Inheritance models an "is a" relationship. A dog is an animal, and a bike is a vehicle, so Bike can inherit from Vehicle. Using inheritance where the relationship is not "is a", such as making a car inherit from its engine, leads to confusing code.

    In most languages a subclass names its parent in its header. In Java the keyword is extends, and in Python the parent is written in brackets after the class name. A subclass calls the parent's constructor with super(), so the shared setup is not repeated.

    A common mistake is assuming a subclass can always see the parent's private data. In Java, private members belong to the parent alone, so the subclass must use the parent's non-private methods to reach them.

    Worked example

    A shop sells bikes, and Bike is a subclass of Vehicle:

    class Vehicle:
        def __init__(self, wheels):
            self.wheels = wheels
    
    class Bike(Vehicle):
        def __init__(self):
            super().__init__(2)
            self.bell = True
    
    1. Creating a Bike calls super().init(2), which runs the Vehicle constructor and sets wheels to 2.
    2. Bike does not declare wheels itself, but it inherits the attribute from Vehicle.
    3. For 3 bikes, the total number of wheels is 3×2=63 \times 2 = 6.

    Check: each bike has 2 wheels, so 3 bikes have 6 wheels in total.

    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) { System.out.println(new Square().value()); } }

      class Shape { int value() { return 2; } }

      class Square extends Shape { }

    • Medium

      What number does this Java code print?

      public class Main { public static void main(String[] args) { System.out.println(new Tree(5).size); } }

      class Plant { int size;

      Plant(int s) { size = s; } }

      class Tree extends Plant { Tree(int s) { super(s + 5); } }

    • Hard

      What number does this Java code print?

      public class Main { public static void main(String[] args) { Device obj = new Phone(); System.out.println(obj.getSize()); } }

      class Device { int size = 2;

      int getSize() { return size; } }

      class Phone extends Device { int size = 7;

      int getSize() { return size; } }