HackerRank Java- Method Overriding 2 (Super Keyword)




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:



  1. import java.util.*;
  2. import java.io.*;
  3. class BiCycle{
  4. String define_me(){
  5. return "a vehicle with pedals.";
  6. }
  7. }
  8. class MotorCycle extends BiCycle{
  9. String define_me(){
  10. return "a cycle with an engine.";
  11. }
  12. MotorCycle(){
  13. System.out.println("Hello I am a motorcycle, I am "+ define_me());
  14. String temp = super.define_me();
  15. System.out.println("My ancestor is a cycle who is "+ temp );
  16. }
  17. }
  18. class Solution{
  19. public static void main(String []args){
  20. MotorCycle M=new MotorCycle();
  21. }
  22. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube