I’m creating a program for a homework assignment that figures out the cost for a train trip that uses an interface:
Train Class:
public class Train implements MassTransit {
public void getCapacity() {
int capacity = 100;
}//end get Capacity
public int getRoundTripCost(int leave, int return_time){
int cost = 0;
cost = (return_time - leave) * 100;
return cost;
} //end getRoundtrip
public static void main(String[] args) {
Train train = new Train();
train.getRoundTripCost(20,25);
}//end of main method
}//end train class
And the MassTransit Method
public interface MassTransit {
public void getCapacity();
public void getRoundTripCost(int leave, int return_time);
}//end of MassTransit interface
When I try to compile the Train class, the errors I get is “Train.java:6: error: Train is not abstract and does not override abstract method getRoundTripCost(int,int) in MassTransit”
And
“Train.java:19: error: getRoundTripCost(int,int) in Train cannot implement getRoundTripCost(int,int) in MassTransit”
I’m a complete Java newbie, and I’m still unfamiliar with Java interfaces. What did I do wrong?
Above overriden method return type should be
voidnotint. Your interface defined method with return typevoid.As per java tutorial