Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language Java . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Java Method Overriding-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To Java
JAVA was developed by James Gosling at Sun Microsystems Inc in the year 1991, later acquired by Oracle Corporation. It is a simple programming language. Java makes writing, compiling, and debugging programming easy. It helps to create reusable code and modular programs.
Java is a class-based, object-oriented programming language and is designed to have as few implementation dependencies as possible. A general-purpose programming language made for developers to write once run anywhere that is compiled Java code can run on all platforms that support Java. Java applications are compiled to byte code that can run on any Java Virtual Machine. The syntax of Java is similar to c/c++.
Link for the Problem – Java Method Overriding – Hacker Rank Solution
Java Method Overriding – Hacker Rank Solution
Problem :
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
Java Method Overriding – Hacker Rank Solution
import java.util.*; class Sports{ String getName(){ return "Generic Sports"; } void getNumberOfTeamMembers(){ System.out.println( "Each team has n players in " + getName() ); } } class Soccer extends Sports{ @Override String getName(){ return "Soccer Class"; } @Override void getNumberOfTeamMembers() { System.out.println("Each team has 11 players in " + getName()); } } public class Solution{ public static void main(String []args){ Sports c1 = new Sports(); Soccer c2 = new Soccer(); System.out.println(c1.getName()); c1.getNumberOfTeamMembers(); System.out.println(c2.getName()); c2.getNumberOfTeamMembers(); } }
Very interesting points you have observed, thankyou for posting.