We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
import java.util.Scanner;
class Sports
{
String getName()
{
return "Generic Sports";
}
void getNumberOfTeamMembers()
{
System.out.println("Each team has n players in "+getName());
}
}
class Soccer extends Sports
{
String getName(){
return "Soccer Class";
}
void getNumberOfTeamMembers()
{
System.out.println("Each team has 11 players in "+getName());
}
}
class Solution
{
public static void main(String[] args)
{
Sports s = new Sports();
Soccer s1 = new Soccer();
System.out.println(s.getName());
s.getNumberOfTeamMembers();
System.out.println(s1.getName());
s1.getNumberOfTeamMembers();
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Method Overriding
You are viewing a single comment's thread. Return to all comments →
import java.util.Scanner; class Sports { String getName() { return "Generic Sports"; } void getNumberOfTeamMembers() { System.out.println("Each team has n players in "+getName()); } } class Soccer extends Sports { String getName(){ return "Soccer Class"; } void getNumberOfTeamMembers() { System.out.println("Each team has 11 players in "+getName()); } } class Solution { public static void main(String[] args) { Sports s = new Sports(); Soccer s1 = new Soccer(); System.out.println(s.getName()); s.getNumberOfTeamMembers(); System.out.println(s1.getName()); s1.getNumberOfTeamMembers();
}