Java Method Overriding 2 HackerRank Solution

Hello Programmers, In this post, you will know how to solve the Java Method Overriding 2 HackerRank Solution. This problem is a part of the HackerRank Java Programming Series.

Ezoicreport this adJava Method Overriding 2 HackerRank Solution
Java Method Overriding 2 HackerRank Solutions

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Java Method Overriding 2 HackerRank Solutions

Problem

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.

Java Method Overriding 2 HackerRank Solutions

import java.util.*;
import java.io.*;
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();
		System.out.println("My ancestor is a cycle who is "+ temp );
	}
}
class Solution{
	public static void main(String []args){
		MotorCycle M=new MotorCycle();
	}
}

Disclaimer: The above Problem (Java Method Overriding 2) is generated by Hackerrank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: Java Instance of keyword HackerRank Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad