Exp 6 Java
Exp 6 Java
Experiment:.2.2
Objective:
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(){
void getNumberOfTeamMembers(){
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:
@Override
String getName(){
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
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
Output Format
Generic Sports
Soccer Class
ProgramCode:
import java.util.*;
class Sports{
String getName(){
return "Generic Sports";
}
void getNumberOfTeamMembers(){
System.out.println( "Each team has n players in " + getName() );
}
}
void getNumberOfTeamMembers(){
System.out.println( "Each team has 11 players in " + getName() );}
}
Output:
3.I learned how override a method and print same statement which are inside method
of super class.