0

I've been given the instructions: Write an interface called Playable, with a method void play();. Let this interface be placed in a package called music.Write a class called Veena which implements Playable interface. Let this class be placed in a package music.string.

I have created the interface and packaged it below:

package music; interface Playable { public void play(); } 

I then created the next class below:

package music.string; import music.Playable; public class Veena implements Playable { public void play() { System.out.println("Veena plays"); } } 

I've played around with this and so far, I either don't get to name the package it's in to "music.string" or I receive an error since Playable is not public and cannot be used outside the interface. How would I import and implement Playable in my class Veena while also packaging Veena in "music.string"?

1

2 Answers 2

1

Make the interface public :

public interface Playable { void play(); } 

A package-private class/interface may only be referenced by classes/interfaces belonging to the exact same package.

Sign up to request clarification or add additional context in comments.

3 Comments

Oh wow... I kept making that change and wondering why it wasn't working. It just occurred to me that I have to recompile Playable.java before compiling Veena.java. I'm so sorry for the stupid question.
Don't be sorry and thanks for the feedback :) But I don't really understand that it may work. Are you sure that you use the default access modifier for Playable and that Playable and Veena are in two distinct packages ? The compile could not success in these conditions.
Also, depending on your IDE, if one project is set as a dependency of the other and auto build is selected, it will automatically build both prior to running.
0

you cannot access interface from different package in case it is not defined public. If you want to keep your interface default you have to move your classes to same package, but as you have an requirement to keep your interface in separate package(which is a good design also) make your interface public.

package music; public interface Playable { void play(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.