Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Tue Jul 31, 2012 4:15 am
by ETS User
class and interface both are public here.how it can be possible in source file ?
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Tue Jul 31, 2012 5:14 am
by admin
Hi,
The question doesn't say that they are in the same file. Further, there is no option that alludes to a compilation failure because of two public classes in the same file, so you can safely assume that that is not an issue in this question.
But you are right that it is confusing. It should be made explicitly clear in the question that both are in separate files.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Sun Aug 12, 2012 1:07 pm
by Javanaut
Yes, two public things in the same source file will not compile. I remembered this after experimenting with the code in Eclipse and it gave an error.
I think something should be said about how the public interface and the public class are defined in separate source files.

Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Wed Dec 11, 2013 5:13 am
by declan_
It's a little ambiguous since the question does not indicate that the files are in the same package.
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Wed Dec 11, 2013 5:44 am
by admin
The question was changed long back to clearly state, "Consider the following class and interface definitions (in separate files)".
Please make sure you are using the latest version of the question bank.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Wed Dec 11, 2013 5:45 am
by admin
declan_ wrote:It's a little ambiguous since the question does not indicate that the files are in the same package.
Since full source code is given there is no ambiguity about the package.
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Wed Jun 25, 2014 1:44 pm
by sarakh
Why is the following OK?
Code: Select all
public class TestClass{
interface J{
int getJ(int a, int b, int c);
}
interface K extends J{
int getJ(int a, int b, int c, int d);
}
}
Why is it OK for an interface that is extending another interface to have the same method, with the same name, with different number of arguments?
Wouldn't a class that wants to implement these two interfaces be now confused on how to implement method getJ?
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Wed Jun 25, 2014 7:32 pm
by admin
No, it would not be confusing (technically, of course) at all because when the method parameter definition changes, it is considered an entirely different method (even if the name of the methods are same.). This is same as overloading.
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Tue Aug 19, 2014 7:23 am
by javinocente
I have tried to run the code:
1. with the interface in the same file
2. with the interface in a different file
in both cases, the code compiles and runs. I am using the latest question bank.
what am I missing?
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Tue Aug 19, 2014 7:50 am
by admin
Javinocente, the question and the explanation are correct. You need to post the exact code that you are trying to compile.
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Sun Jul 05, 2015 8:52 pm
by evafang2008
public class Sample implements IInt, aaa{
public static void main(String[] args){
Sample s = new Sample(); //1
int j = s.thevalue; //2
int k = IInt.thevalue; //3
int l = thevalue; //4
}
}
public interface IInt{
int thevalue = 0;
}
For this question , what if add the following and add another interface aaa :
public interface aaa{
int thevalue = 0;
}
It will not compile because j and i cannot figure out thevalue in which interface.
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Sun Jul 05, 2015 9:11 pm
by admin
What happened when you tried it out?
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Mon Jul 06, 2015 4:06 pm
by evafang2008
I just want to try, when can directly use the static field name and when it cannot. In the mock question, it only has one interface, so it is ok. I try adding to another one interface with a static field named the same. Then it did not compile. I think for line 4 code: "int l = thevalue;" is not accurate. it should add a class name before the field name.
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Mon Jul 06, 2015 9:18 pm
by admin
If there is an ambiguity in the name (i.e. if there are multiple fields with the same name accessible in a class due to inheritance or interface implements), then you cannot use a simple name to access a field because the compile will not know which one are you referring to. In that case, you have to use the class name or interface name to clarify.
You should try out various scenarios to understand this. We have covered this topic in some questions.
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Wed Nov 15, 2017 3:12 pm
by SeoaneR
In relation to
Code: Select all
public class Sample implements IInt{
public static void main(String[] args){
Sample s = new Sample(); //1
int j = s.thevalue; //2
int k = IInt.thevalue; //3
int l = thevalue; //4
}
}
public interface IInt{ int thevalue = 0; }
I have type this code out with the interface in a different package and imported the package when creating
the class and implemented the interface and I don't get any compilation problems.
When i do a System.out on the variable I . I get a result .
The answer says that i should get a Compilation error.
Please advise
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Wed Nov 15, 2017 8:56 pm
by admin
No, the correct answer (i.e. option 5) says, "It will compile and run without any problem."
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Mon May 14, 2018 10:21 pm
by Sai Krishna Datt
Hello,
I know interface variables are public static and final . If we implement interface why do static member filed(thevalue here) inherited to the class(Sample class here) which implement the interface .
Regards,
Sai Krishna
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Mon May 14, 2018 10:49 pm
by admin
That is how the designers designed the language. No other reason.
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Thu Jun 18, 2020 1:17 pm
by lolkin4777
It will not to compile because class in Java 8 don't inherits static fields & methods from interface.
Aswer:
It will give an error at compile time at line //2.
please fix it.
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Thu Jun 18, 2020 4:29 pm
by admin
Not sure what exactly you think should be fixed. The given answer is correct.
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Wed Jul 28, 2021 10:06 am
by Leonid
hi
I see this question:
Consider the following class and interface definitions (in separate files):
public class Sample implements IInt{
public static void main(String[] args){
Sample s = new Sample(); //1
int j = s.thevalue; //2
int k = IInt.thevalue; //3
int l = thevalue; //4
}
}
public interface IInt{ int thevalue = 0; }
What will happen when the above code is compiled and run?
I know that both codes are in separate files. And we haven't import package, then thevalue will be not access in the Sample class. I thought that import is hidden in our case, but I saw special row : (in separate files). Then in this case it is trick, And we need import word. And I say: It will give an error at compile time at line 2; But correct answer is: It will compile and run without any problem. I don't know, why?
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Wed Jul 28, 2021 10:17 am
by admin
Why do you think you need import statement?
Full code for both the files is given. There is no package statement in either of them. Therefore, both the class and the interface belong to the default package. There is no need to have an import statement.
Please understand that the problem statement says the code is in separate files. Not in different packages!
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Posted: Wed Jul 28, 2021 12:51 pm
by Leonid
Oh yes, right, not in different packages. I imagined it was the same. But I remembered now. Thank you.