I'm trying to learn how use interfaces. My package hierarchy looks like this:
InterfacePackages.PNG
SelfDrivable.java
package interfaces.business; import java.util.Scanner; public interface SelfDrivable { public static final int SUPERSONIC = 768;//This constant should be in a final class instead of here. void showMusicPlaylists(); default String setDestination(String destination) { System.out.println("Where do you want to go?"); Scanner dest = new Scanner(System.in); destination = dest.nextLine(); return destination; } public abstract double totalGasCost(int MPG, int numGasStops); void drive(boolean arrived, int milesToGo); void wakeUp(); void honkHorn(); }
SelfDrivableExtender.java
package interfaces.business; import java.util.Scanner; public interface SelfDrivableExtender extends SelfDrivable{ default String setDestination(String destination) { SelfDrivable.super.setDestination(destination); System.out.println("Now defining the extended verson of the setDestination method."); System.out.println("Where do you want to go?"); Scanner dest = new Scanner(System.in); destination = dest.nextLine(); return destination; } }
DrivingMusic.java
package interfaces.business; public interface DrivingMusic { String PL1 = "JazzJackrabbitOST"; String PL2 = "DonkeyKongCountryOST"; String PL3 = "MegaDeth"; String PL4 = "Offspring"; }
Printable.java
package interfaces.business; public interface Printable { void print(); }
Driver.java
package interfaces.business; import java.util.Scanner; public class Driver implements Printable,DrivingMusic{ private int DriverID; private String firstName; private String lastName; private String playlist; public Driver(int DriverID, String firstName, String lastName, String playlist) { this.DriverID = DriverID; this.firstName = firstName; this.lastName = lastName; this.playlist = playlist; } @Override public void print() { System.out.println("Select a playlist: PL1, PL2, PL3, or PL4."); String playlist = "No Music Selected."; Scanner input = new Scanner(System.in); playlist = input.nextLine(); if(playlist.equals("PL1")) { System.out.println("You have chosen to listen to the " + DrivingMusic.PL1); } else if(playlist.equals("PL2")) { System.out.println("You have chosen to listen to the " + DrivingMusic.PL2); } else if(playlist.equals("PL3")) { System.out.println("You have chosen to listen to the " + DrivingMusic.PL3); } else if(playlist.equals("PL4")) { System.out.println("You have chosen to listen to the " + DrivingMusic.PL4); } else { System.out.println("Invalid music playlist selected."); } } }
Main.java
Line 9 says, "The type Main must implement the inherited abstract method Driver.getMinorVersion()". I don't have a clue what that means.package interfaces.ui; import java.sql.Driver; import interfaces.business.DrivingMusic; import interfaces.business.Printable; import interfaces.business.SelfDrivable; import interfaces.business.SelfDrivableExtender; public class Main implements DrivingMusic, Driver, SelfDrivable, SelfDrivableExtender{ private static void printPlaylists(Printable p, DrivingMusic dm, int count) { for(int i = 0; i < count; i++) { p.print(); } } public static void main(String[] args) { //printPlaylists(p, dm, count); //Driver.print(); System.out.println("Lets print our playlist strings again:"); System.out.println(DrivingMusic.PL1 + ", " + DrivingMusic.PL2 + ", " + DrivingMusic.PL3 + ", " + DrivingMusic.PL4); } }
For all 3 arguments in Line 18, it says that the arguments cannot be resolved to a variable. I don't understand why that is, since I defined the method right in the same class.
Line 19 says, "The method print() is undefined for the type Driver". I know that the print method in the Driver class is overridden, but why can't I call it from there?