HackerRank Java- Method Overriding




omplete the code in your editor by writing an overridden getNumberOfTeamMembers method that prints the same statement as the superclass' getNumberOfTeamMembers method, except that it replaces n with 11 (the number of players on a Soccer team).



  1. import java.util.*;
  2. class Sports{
  3. String getName(){
  4. return "Generic Sports";
  5. }
  6. void getNumberOfTeamMembers(){
  7. System.out.println( "Each team has n players in " + getName() );
  8. }
  9. }
  10. class Soccer extends Sports{
  11. @Override
  12. String getName(){
  13. return "Soccer Class";
  14. }
  15. @Override
  16. void getNumberOfTeamMembers() {
  17. System.out.println("Each team has 11 players in " + getName());
  18. }
  19. }
  20. public class Solution{
  21. public static void main(String []args){
  22. Sports c1 = new Sports();
  23. Soccer c2 = new Soccer();
  24. System.out.println(c1.getName());
  25. c1.getNumberOfTeamMembers();
  26. System.out.println(c2.getName());
  27. c2.getNumberOfTeamMembers();
  28. }
  29. }




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