When a method in a subclass overrides a method in superclass, it is still possible to call the overridden method using super keyword - Hacker Rank Solution.

When a method in a subclass overrides a method in superclass, it is still possible to call the overridden method using super keyword. If you write super.func() to call the function func(), it will call the method that was defined in the superclass.

You are given a partially completed code in the editor. Modify the code so that the code prints the following text:

Hello I am a motorcycle, I am a cycle with an engine.
My ancestor is a cycle who is a vehicle with pedals.


SOLUTION : -


import java.io.*;
import java.util.*;

class Cycle {
    String define_me() {
        return "a vehicle with pedals.";
    }
}

class Motorcycle extends Cycle {
    @Override
    String define_me() {
        return "a cycle with an engine.";
    }

    Motorcycle() {
        System.out.println("Hello I am a motorcycle, I am " + define_me());
        String temp = super.define_me();
        System.out.println("My ancestor is a cycle who is " + temp);
    }
}
public class Solution {

    public static void main(String[] args) {
        Motorcycle m = new Motorcycle();
    }
}

Popular posts from this blog

You are given a date. You just need to write the method getDay which returns the day on that date - Hacker Rank Solution.

Java's System.out.printf function can be used to print formatted output. The purpose of this exercise is to test your understanding of formatting output using printf. To get you started, a portion of the solution is provided for you in the editor; you must format and print the input to complete the solution.