Method Overriding2 Super Keyword Solution

In the context of Java, method overriding occurs when a subclass provides a specific implementation of a method that is already provided by its parent class. The 'super' keyword is used to call the method of the parent class from the subclass. This code defines two classes, 'BiCycle' and 'MotorCycle', where 'MotorCycle' is a subclass of 'BiCycle'. Both classes have a method named 'define_me'.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the Method Overriding2 Super Keyword Solution HackerRank solution using CodeRankGPT:

The 'MotorCycle' class overrides the 'define_me' method of the 'BiCycle' class. In the constructor of the 'MotorCycle' class, it first prints its own definition by calling 'define_me'. Then, it uses the 'super' keyword to call the 'define_me' method of the 'BiCycle' class (its parent class) and prints the definition of its ancestor. The 'main' method creates an instance of the 'MotorCycle' class, triggering these print statements.


/**
 * 
 */
package com.javaaid.hackerrank.solutions.languages.java.oop;

/**
 *
 */

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

class MotorCycle extends BiCycle {
	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(); // Fix this line
		System.out.println("My ancestor is a cycle who is " + temp);
	}

}

public class JavaMethodOverriding2SuperKeyword {
	public static void main(String[] args) {
	 new MotorCycle();
	}
}

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform