This is a really novice question but I am not able to implement any default-access interface from the first source file('F:/Java_Practice/src/listpage1/interfaceTest/InterfaceCan.java;`) on the public class in the second source file(F:/Java_Practice/src/listpage1/interfaceTest/InterfaceCan1.java;). Please throw light on this. Thankyou!
/* Source file name listpage1.interfaceTest.InterfaceCan1.java */
package listpage1.interfaceTest;
import static java.lang.System.out;
public interface InterfaceCan1
{
int iCan_var = 2;
void iCanMethod();
}
class PackageInfo
{
void packInfo()
{
out.println("This package is meant to contain all the interfaces used for testing the behavior of interfaces.");
}
}
interface I1
{
int i1_var = 1;
void i1Method();
}
interface I1x extends I1
{
int i1x_var = 10;
void i1xMethod();
}
interface I2x extends InterfaceCan1
{
int i2x_var = 20;
void i3xMethod();
}
interface I3
{
int I3 = 3;
void i3Method();
}
//Another source file: listpage1.interfaceTest.InterfaceTest.java
package listpage1.interfaceTest;
import static java.lang.System.out;
public class InterfaceTest implements I1//This line is throwing error
{
public void i1Method()
{
out.println("Inside the method InterfaceTest.i1Method()");
}
public static void main(String[] s)
{
TestInterface1 ob1 = new TestInterface1();
out.println(ob1.i_var);
ob1.iMethod();
out.println(ob1.iCan_var);
ob1.iCanMethod();
}
}
interface I
{
int i_var = 0;
void iMethod();
}
class TestInterface1 implements I, InterfaceCan1, I1
{
public void iMethod()
{
out.println("Inside the method TestInterface1.iMethod().");
}
public void iCanMethod()
{
out.println("Inside the method TestInterface1.iCanMethod()");
}
public void i1Method()
{
out.println("Inside the method TestInterface1.i1Method()");
}
}
class InterfaceTest2 implements I1
{
public void i1Method()
{
out.println("Inside the method TestInterface1.i1Method()");
}
}
Compilation Error:
listpage1\interfaceTest\InterfaceTest.java:6: error: cannot find symbol
public class InterfaceTest implements I1
^
symbol: class I1
1 error
Thanks a lot for help in the comments. I finally figured out why I was not abel to compile the program. In order for the Interface to be detected while compiling using -d option (by which the .class files are placed in a different directory)
For example: My Dir structure used:
F:/Java_Practice/src
This held all the .java files. I was compiling all the files from this folder and placing the resulting .class
files in F:/Java_Practice/class
folder by using the following command:
"F:\Java_Practice\src>javac -d F:/Java_Practice/class listpage1/interfaceTest/InterfaceTest.java
"(Refer to code in question)
The .class
of that interface and all other classes which got placed in another directory(In this case:'F:/Java_Practice/class'). Now that directory needs to be specified while compiling the .java file using the -classpath option like so:
F:\Java_Practice\src>javac -d F:/Java_Practice/class -classpath .;F:/Java_Practice/class listpage1/interfaceTest/InterfaceTest.java
Interestingly If you miss the -classpath option while compiling you will still be able to access all the public classes and public interfaces! However to access default-access classes and interfaces you will have to include -classpath
in javac
command
Please go through this link for more details on setting classpath: http://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html