Hackerrank Java Method Overriding Solution
1 min read

Hackerrank Java Method Overriding Solution

Hackerrank Java Method Overriding Solution

When a subclass inherits from a superclass, it also inherits its methods; however, it can also override the superclass methods (as well as declare and implement new ones). Consider the following Sports class:class Sports{    String getName(){        return "Generic Sports";    }    void getNumberOfTeamMembers(){        System.out.println( "Each team has n players in " + getName() );    }}

Next, we create a Soccer class that inherits from the Sports class. We can override the getName method and return a different, subclass-specific string:class Soccer extends Sports{    @Override    String getName(){        return "Soccer Class";    }}

Note: When overriding a method, you should precede it with the @Override annotation. The parameter(s) and return type of an overridden method must be exactly the same as those of the method inherited from the supertype.


Task
Complete 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  with  (the number of players on a Soccer team).

Output Format

When executed, your completed code should print the following:

Generic Sports
Each team has n players in Generic Sports
Soccer Class
Each team has 11 players in Soccer Class

Solution in java8

Approach 1.

    @Override
     void getNumberOfTeamMembers(){
        System.out.printf( "Each team has %d players in %s",11, getName() );
    }

Approach 2.

    // Write your overridden getNumberOfTeamMembers method here
	@Override
	void getNumberOfTeamMembers() {
		System.out.println("Each team has 11 players in " + getName());
	}

Approach 3.

    // Write your overridden getNumberOfTeamMembers method here

@Override
void getNumberOfTeamMembers () {
       System.out.println( "Each team has 11 players in " + getName() );
}

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×