Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Multiple scope problems with interfaces

  1. #1
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Multiple scope problems with interfaces

    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
    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);
    	}
    }
    Line 9 says, "The type Main must implement the inherited abstract method Driver.getMinorVersion()". I don't have a clue what that means.

    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?
    Last edited by SamJava_the_Hut; June 4th, 2019 at 12:42 PM. Reason: I fixed the problem in the SelfDrivableExtender interface. There are only errors in my Main class now.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Multiple scope problems with interfaces

    Main must implement the inherited abstract method Driver.getMinorVersion()
    Main implements the interface sql.Driver that defines that method as abstract. It needs a definition for all the methods in the Driver interface.

    arguments cannot be resolved to a variable
    What variable was in the full text of the error message? Where is that variable defined?

    The method print() is undefined for the type Driver"
    Is that referring to the line that is now commented out?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Multiple scope problems with interfaces

    Quote Originally Posted by Norm View Post
    Is that referring to the line that is now commented out?
    Lets forget about that. I was misusing it anyway

    Quote Originally Posted by Norm View Post
    What variable was in the full text of the error message? Where is that variable defined?
    If you look at my Main class (of which the red around the Main keyword of the class signature is now gone, because I implemented the methods of the implemented interfaces), I have a private void printPlaylists method defined right before the main method. My book told me it's possible to use an interface object as an argument, but I'm having difficulty passing the arguments to the printPlaylists method call in the main method, despite both the printPlaylists and main methods being in the same class.

    Also, I don't understand why that is possible, because from what I understand, you can't create instances (assuming objects and instances are the same thing) of interfaces, but rather can only define their methods in the classes that you choose to implement those interfaces in.


    That creates a new problem, which shouldn't happen with Java 8 (unless I'm missing something). If you're not allowed to create instances of interfaces, how are you supposed to call non-static default methods from interfaces?

    The errors are commented on lines 14, 18, 20, and 22:
    package interfaces.ui;
    import interfaces.business.*;
    import java.util.Scanner;
     
    public class Main implements DrivingMusic, Printable, SelfDrivable{
     
    	private 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);//none of the arguments can be resolved to a variable
     
    		//DrivingMusic.showMusicPlaylists();//call the static version of the method
     
    		DrivingMusic x = new DrivingMusic();//Cannot instantiate the type DrivingMusic
    		x.showMusicPlaylists();
    		System.out.println(super.showMusicPlaylists);//Cannot use super in a static context
     
    		select();//Cannot make a static reference to the non-static method select() from the type Main
     
    		System.out.println("Lets also make sure we can both import and output our constants "
    				+ "defined in the DrivingMusic interface directly from there:");
    		System.out.println(DrivingMusic.PL1 + ", " + DrivingMusic.PL2 + ", " +
    							DrivingMusic.PL3 + ", " + DrivingMusic.PL4);
    	}
     
    	@Override
    	public double totalGasCost(int MPG, int numGasStops) {
    		// TODO Auto-generated method stub
    		return 0;
    	}
     
    	@Override
    	public void drive(boolean arrived, int milesToGo) {
    		// TODO Auto-generated method stub		
    	}
     
    	@Override
    	public void wakeUp() {
    		// TODO Auto-generated method stub		
    	}
     
    	@Override
    	public void honkHorn() {
    		// TODO Auto-generated method stub		
    	}	
     
    	@Override
    	public void select() {
    		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.");
    		}		
    	}
     
    	@Override
    	public void print() {
    		// TODO Auto-generated method stub		
    	}
    }

    And here are the interfaces I'm trying to use:

    DrivingMusic.java
    package interfaces.business;
     
    import java.util.Scanner;
     
    public interface DrivingMusic extends Printable{
     
    	String PL1 = "JazzJackrabbitOST";
    	String PL2 = "DonkeyKongCountryOST";
    	String PL3 = "MegaDeth";
    	String PL4 = "Offspring";
     
    	String PL_list = ("Our playlists are: " + PL1 + ", " + PL2 + ", " + PL3 + ", " + PL4);
     
    	default void showMusicPlaylists() {
    		System.out.println(PL_list);
    	}
    	/*
    	static void showMusicPlaylists() {
    		System.out.println("Now printing our music playlists from the static version of the "
    				+ "showMusicPlaylists method defined in the DrivingMusic interface:");
    		System.out.println(PL_list);
    	}
    	*/
    }

    Printable.java
    package interfaces.business;
     
    public interface Printable {
     
    	void print();
    	void select();
    }

    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.
     
    	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();	
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Multiple scope problems with interfaces

    arguments can be resolved to a variable
    Please copy and post here the FULL text of the error messages, not just a short reference to it.

    Cannot instantiate the type DrivingMusic
    Yes it is an interface not a class.

    cannot use super in a static context
    Self explanatory.


    When posting error messages, please don't bury the error messages in the source.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Multiple scope problems with interfaces

    Quote Originally Posted by Norm View Post
    Yes it is an interface not a class.
    You're dead right. So why are default methods (non-static interface methods with a body) useful then? How do you implement them? Even if you can pass them around interfaces, what good does that do if you can't call them in your program's main method non-statically? In my updated code below, I have even tried overriding my methods outside of the main method but in still in the same Main class, and yet my program is still complaining about me trying to make a static reference to a non-static method when I try to call them in my code:

    Main.java
    package interfaces.ui;
    import interfaces.business.*;
    import java.util.Scanner;
     
    public class Main implements DrivingMusic, Printable, SelfDrivable{
     
    	private void printMultiple(Printable p, int count) {
    		for(int i = 0; i < count; i++) {
    			p.print();
    		}
    	}
     
    	public static void main(String[] args) {
     
    		Driver z = new Driver(5580,"Sam","Peterson");		
     
    		printMultiple(z, 2);
     
    		//DrivingMusic.showMusicPlaylists();//call the static version of the method
     
    		showMusicPlaylists();
     
    		/*[Code to call the non-static version of showMusicPlaylists here]*/
     
    		select();
     
    		System.out.println("Lets also make sure we can both import and output our constants "
    				+ "defined in the DrivingMusic interface directly from there:");
    		System.out.println(DrivingMusic.PL1 + ", " + DrivingMusic.PL2 + ", " +
    							DrivingMusic.PL3 + ", " + DrivingMusic.PL4);
    	}
     
    	@Override
    	public double totalGasCost(int MPG, int numGasStops) {
    		// TODO Auto-generated method stub
    		return 0;
    	}
     
    	@Override
    	public void drive(boolean arrived, int milesToGo) {
    		// TODO Auto-generated method stub		
    	}
     
    	@Override
    	public void wakeUp() {
    		// TODO Auto-generated method stub		
    	}
     
    	@Override
    	public void honkHorn() {
    		// TODO Auto-generated method stub		
    	}	
     
    	@Override
    	public void select() {
    		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.");
    		}		
    	}
     
    	@Override
    	public void showMusicPlaylists() {
    		System.out.println(PL_list);
    	}
     
    	@Override
    	public void print() {
    		// TODO Auto-generated method stub		
    	}
    }

    DrivingMusic.java
    package interfaces.business;
     
    import java.util.Scanner;
     
    public interface DrivingMusic extends Printable{
     
    	String PL1 = "JazzJackrabbitOST";
    	String PL2 = "DonkeyKongCountryOST";
    	String PL3 = "MegaDeth";
    	String PL4 = "Offspring";
     
    	String PL_list = ("Our playlists are: " + PL1 + ", " + PL2 + ", " + PL3 + ", " + PL4);
     
    	default void showMusicPlaylists() {
    		System.out.println(PL_list);
    	}
    	/*
    	static void showMusicPlaylists() {
    		System.out.println("Now printing our music playlists from the static version of the "
    				+ "showMusicPlaylists method defined in the DrivingMusic interface:");
    		System.out.println(PL_list);
    	}
    	*/
    }

    Printable.java
    package interfaces.business;
     
    public interface Printable {
     
    	void print();
    	void select();
    }

    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.
     
    	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();	
    }

    Driver.java
    package interfaces.business;
    public class Driver{
     
    	private int DriverID;
    	private String firstName;
    	private String lastName;
     
    	public Driver(int DriverID, String firstName, String lastName) {
    		this.DriverID = DriverID;
    		this.firstName = firstName;
    		this.lastName = lastName;
    	}
     
    	public void setDriverID(int DriverID) {
    		this.DriverID = DriverID;
    	}
    	public int getDriverID() {
    		return DriverID;
    	}
     
    	public void setFirstName(String firstName) {
    		this.firstName = firstName;
    	}
    	public String getFirstName() {
    		return firstName;
    	}
     
    	public void setLastName(String lastName) {
    		this.lastName = lastName;
    	}
    	public String getLastName() {
    		return lastName;
    	}			
    }

    Quote Originally Posted by Norm View Post
    Please copy and post here the FULL text of the error messages, not just a short reference to it.
    Line 17: "The method printMultiple(Printable, int) in the type Main is not applicable for the arguments (Driver, int)"

    Line 21: "Cannot make a static reference to the non-static method showMusicPlaylists() from the type Main"

    Line 25: "Cannot make a static reference to the non-static method select() from the type Main"

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Multiple scope problems with interfaces

    Try reading the tutorial: https://docs.oracle.com/javase/tutor...interface.html

    Line 17: "The method printMultiple(Printable, int) in the type Main is not applicable for the arguments (Driver, int)"
    The calling code and the definition for the method are different. Make sure they are the same


    Line 21: "Cannot make a static reference to the non-static method showMusicPlaylists() from the type Main"

    Line 25: "Cannot make a static reference to the non-static method select() from the type Main"
    You need a reference to an instance of the class to call non-static methods:
       public void showMusicPlaylists() {   // non-static method
    ...
    	public void select() {  // non-static method
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Multiple scope problems with interfaces

    Thank you Norm. On to the next thing. Even with the tutorial link you gave me, I'm still having trouble with calling default methods. Here's my updated code:

    Printable.java
    package interfaces.business;
     
    public interface Printable{
     
    	void print();
    	void printMusicPlaylists();
    	void select();	
    }

    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.
     
    	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();	
    }

    DrivingMusic.java
    package interfaces.business;
     
    import java.time.ZonedDateTime;
    import java.util.Scanner;
     
    public interface DrivingMusic extends Printable{
     
    	String PL1 = "JazzJackrabbitOST";
    	String PL2 = "DonkeyKongCountryOST";
    	String PL3 = "MegaDeth";
    	String PL4 = "Offspring";
     
    	String PL_list = ("Our playlists are: " + PL1 + ", " + PL2 + ", " + PL3 + ", " + PL4);
     
    	default void showMusicPlaylists() {
    		System.out.println("Calling the default showMusicPlaylists method defined in the DrivingMusic interface.");
    		System.out.println(PL_list);
    	}		
    }

    CarClock.java
    package interfaces.business;
    import java.time.*;
     
    public interface CarClock {
     
    	//ZonedDateTime getZonedDateTime(String zoneStr);//abstract method
     
    	LocalDateTime getLocalDateTime();	
     
    	static ZoneId getZoneId(String zoneStr) {
    		try {
    			return ZoneId.of(zoneStr);
    		}catch(DateTimeException e) {
    			System.err.println("Invalid time zone: " + zoneStr + "; using default time zone instead.");
    			return ZoneId.systemDefault();
    		}
    	}
     
    	default ZonedDateTime getZonedDateTime(String zoneStr) {
    		return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneStr));
    	}
    }

    Driver.java
    package interfaces.business;
     
    import java.util.Scanner;
    import java.time.*;
     
    public class Driver implements Printable, DrivingMusic, CarClock{
     
    	private int DriverID;
    	private String firstName;
    	private String lastName;
     
    	public Driver(int DriverID, String firstName, String lastName) {
    		this.DriverID = DriverID;
    		this.firstName = firstName;
    		this.lastName = lastName;
    	}
     
    	public void setDriverID(int DriverID) {
    		this.DriverID = DriverID;
    	}
    	public int getDriverID() {
    		return DriverID;
    	}
     
    	public void setFirstName(String firstName) {
    		this.firstName = firstName;
    	}
    	public String getFirstName() {
    		return firstName;
    	}
     
    	public void setLastName(String lastName) {
    		this.lastName = lastName;
    	}
    	public String getLastName() {
    		return lastName;
    	}
     
    	@Override
    	public void print() {
    		System.out.println("calling the print method definition from the Driver class.");
    		System.out.println();
    	}
     
    	@Override
    	public void printMusicPlaylists() {
    		System.out.println("calling the printMusicPlaylists method definition from the Driver class.");
    		System.out.println(DrivingMusic.PL_list);
    		System.out.println();
    	}
     
    	@Override
    	public void select() {
    		System.out.println("calling the select method definition from the Driver class.");
    		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.");
    		}
    		System.out.println();
    	}
     
    	public void showMusicPlaylists();
     
    	public ZonedDateTime getZonedDateTime(String zoneStr);
    }

    Main.java
    package interfaces.ui;
    import interfaces.business.*;
    import java.util.Scanner;
    import java.time.*;
    //Remember, we don't need to implement the Printable interface in this class,
    //because the DrivingMusic interface is already extending the Printable interface.
    public class Main implements DrivingMusic, SelfDrivable, CarClock{
     
    	private static void printMultiple(Printable p, int count) {
    		for(int i = 0; i < count; i++) {
    			System.out.println("From the for loop in the printMultiple method we are now");				
    			p.print();			
    		}
    	}
     
    	public static void main(String[] args) {
     
    		Driver z = new Driver(5580,"Sam","Peterson");		
     
    		printMultiple(z, 3);
     
    		//DrivingMusic.showMusicPlaylists();//call the static version of the method
     
    		//You need a reference to an instance of the class to call non-static methods:
    		z.print();
    		z.printMusicPlaylists();
    		z.select();
    		z.showMusicPlaylists();
    		z.getZonedDateTime(zoneStr);
     
    		System.out.println("Again, our playlists are:");
    		System.out.println(DrivingMusic.PL1 + ", " + DrivingMusic.PL2 + ", " +
    							DrivingMusic.PL3 + ", " + DrivingMusic.PL4);
    	}
     
    	@Override
    	public double totalGasCost(int MPG, int numGasStops) {
    		// TODO Auto-generated method stub
    		return 0;
    	}
     
    	@Override
    	public void drive(boolean arrived, int milesToGo) {
    		// TODO Auto-generated method stub		
    	}
     
    	@Override
    	public void wakeUp() {
    		// TODO Auto-generated method stub		
    	}
     
    	@Override
    	public void honkHorn() {
    		// TODO Auto-generated method stub		
    	}
     
    	@Override
    	public void showMusicPlaylists() {
    		System.out.println("Calling the showMusicPlaylists method definition from the Main class.");
    		System.out.println(PL_list);
    	}
     
    	@Override
    	public void print() {
    		// TODO Auto-generated method stub
    		System.out.println("Calling the print method definition from the Main class.");
    	}
     
    	@Override
    	public void select() {
    		// TODO Auto-generated method stub		
    	}	
     
    	@Override
    	public void printMusicPlaylists() {
    		// TODO Auto-generated method stub
    	}		
    }

    Lines 78 and 80 in Driver.java both have the same error, "This method requires a body instead of a semicolon".

    Line 29 in Main.java has an error that says, "zoneStr cannot be resolved to a variable".

    You can't instantiate methods directly from an interface, they have to be defined in a class and called from there. I get that. But why does it keep telling me they need a body? They're default methods. DEFAULT! That means they already have a body in their interfaces. So what is going on here?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Multiple scope problems with interfaces

    Lines 78 and 80 in Driver.java both have the same error, "This method requires a body instead of a semicolon".
    What is the purpose for coding those two statements? Did you try giving the methods bodies? {} instead of ;

    Line 29 in Main.java has an error that says, "zoneStr cannot be resolved to a variable".
    Where is the variable: zoneStr defined? The compiler can not find its definition.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Multiple scope problems with interfaces

    Quote Originally Posted by Norm View Post
    What is the purpose for coding those two statements? Did you try giving the methods bodies? {} instead of ;
    Norm, that's not the point. Giving a method a body defines the method (or in this case, would redefine a default method). What would be the point of defining a default method in an interface ahead of time if you weren't going to call it as it is?

    What I'm looking for a syntax solution of how to call default methods, since we both already understand that interfaces cannot be instantiated directly.

    I kind of found a solution:

    package interfaces.business;
     
    import java.util.Scanner;
    import java.time.*;
     
    public class Driver implements Printable, DrivingMusic, CarClock{
     
    	private int DriverID;
    	private String firstName;
    	private String lastName;
     
    	public Driver(int DriverID, String firstName, String lastName) {
    		this.DriverID = DriverID;
    		this.firstName = firstName;
    		this.lastName = lastName;
    	}
     
    	public void setDriverID(int DriverID) {
    		this.DriverID = DriverID;
    	}
    	public int getDriverID() {
    		return DriverID;
    	}
     
    	public void setFirstName(String firstName) {
    		this.firstName = firstName;
    	}
    	public String getFirstName() {
    		return firstName;
    	}
     
    	public void setLastName(String lastName) {
    		this.lastName = lastName;
    	}
    	public String getLastName() {
    		return lastName;
    	}
     
    	@Override
    	public void print() {
    		System.out.println("calling the print method definition from the Driver class.");
    		System.out.println();
    	}
     
    	@Override
    	public void printMusicPlaylists() {
    		System.out.println("calling the printMusicPlaylists method definition from the Driver class.");
    		System.out.println(DrivingMusic.PL_list);
    		System.out.println();
    	}
     
    	@Override
    	public void select() {
    		System.out.println("calling the select method definition from the Driver class.");
    		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.");
    		}
    		System.out.println();
    	}
     
    	public void callshowMusicPlaylists() {
    		DrivingMusic.super.showMusicPlaylists();
    	}
     
    	public ZonedDateTime callgetZonedDateTime() {
    		return CarClock.super.getZonedDateTime(zoneStr);
    	}	
    }

    Except line 83 still has the same error as below:

    Quote Originally Posted by Norm View Post
    Where is the variable: zoneStr defined? The compiler can not find its definition.
    By removing that zoneStr argument from the getZoneDateTime(zoneStr) method call on line 29 of Main.java, you would get the following error:

    "The method getZonedDateTime(String) in the type Driver is not applicable for the arguments ()"

    And rightfully so, as the method definition(s) in the CarClock.java interface file have that argument in them. Method definitions and method calls must both have the same sequence of arguments in their method signatures in order to work, so I was just trying to obey that rule by passing the zoneStr argument in. That's why I don't understand why Main.java doesn't recognize that argument.

    And as you can see in the CarClock.java interface file, not only is the zoneStr string passed around, but even the getZoneDateTime(zoneStr) method definition is default! This nightmare won't end until I learn how to call default methods as they are.
    Last edited by SamJava_the_Hut; June 12th, 2019 at 10:50 PM. Reason: Fixed the default method call of type void.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Multiple scope problems with interfaces

    line 83 still has the same error as below:
    Where is the variable: zoneStr defined? The compiler can not find its definition in scope where it is used.

    looking for a syntax solution of how to call default methods
    I suggest that you write a smaller, simpler test case that defines an interface with a default method and defines a class that implements that interface and calls the method. Nothing more than needs be there for the code to execute.
    The posted code has too many other things in it that makes it confusing.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Scope and parameters
    By irishkid in forum Java Theory & Questions
    Replies: 1
    Last Post: December 8th, 2012, 09:52 AM
  2. [SOLVED] Out of scope problem
    By lf2killer in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 21st, 2012, 04:28 AM
  3. Multiple Problems
    By ZeroLRS in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 16th, 2011, 07:33 AM
  4. variabe not within scope
    By brainwave in forum Java Servlet
    Replies: 0
    Last Post: April 17th, 2010, 05:51 AM
  5. Implementing Multiple Interfaces with Generics
    By darkestfright in forum Collections and Generics
    Replies: 5
    Last Post: February 10th, 2010, 08:44 PM