About Question enthuware.ocajp.i.v7.2.1127 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1127 :
class and interface both are public here.how it can be possible in source file ?
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1127 :
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.
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.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1127 :
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.
I think something should be said about how the public interface and the public class are defined in separate source files.
-
- Posts: 1
- Joined: Wed Dec 11, 2013 4:05 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1127 :
It's a little ambiguous since the question does not indicate that the files are in the same package.
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1127 :
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.
Please make sure you are using the latest version of the question bank.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Since full source code is given there is no ambiguity about the package.declan_ wrote:It's a little ambiguous since the question does not indicate that the files are in the same package.
If you like our products and services, please help us by posting your review here.
-
- Posts: 23
- Joined: Fri Jun 20, 2014 3:12 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Why is the following OK?
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?
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);
}
}
Wouldn't a class that wants to implement these two interfaces be now confused on how to implement method getJ?
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1127 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Tue Aug 19, 2014 7:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1127 :
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?
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?
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Javinocente, the question and the explanation are correct. You need to post the exact code that you are trying to compile.
If you like our products and services, please help us by posting your review here.
-
- Posts: 9
- Joined: Fri Jun 26, 2015 11:15 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1127 :
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.
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.
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1127 :
What happened when you tried it out?
If you like our products and services, please help us by posting your review here.
-
- Posts: 9
- Joined: Fri Jun 26, 2015 11:15 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1127 :
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.
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1127 :
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.
You should try out various scenarios to understand this. We have covered this topic in some questions.
If you like our products and services, please help us by posting your review here.
-
- Posts: 8
- Joined: Tue Nov 07, 2017 8:23 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1127 :
In relation to
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
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; }
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
Last edited by admin on Wed Nov 15, 2017 8:52 pm, edited 1 time in total.
Reason: Edited to put code inside [code][/code]
Reason: Edited to put code inside [code][/code]
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1127 :
No, the correct answer (i.e. option 5) says, "It will compile and run without any problem."
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Sat May 05, 2018 10:38 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1127 :
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
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
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1127 :
That is how the designers designed the language. No other reason.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Thu Jun 18, 2020 1:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1127 :
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.
Aswer:
It will give an error at compile time at line //2.
please fix it.
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Not sure what exactly you think should be fixed. The given answer is correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 9
- Joined: Mon Jun 28, 2021 4:50 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1127 :
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?
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?
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1127 :
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!
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!
If you like our products and services, please help us by posting your review here.
-
- Posts: 9
- Joined: Mon Jun 28, 2021 4:50 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1127 :
Oh yes, right, not in different packages. I imagined it was the same. But I remembered now. Thank you.
Who is online
Users browsing this forum: No registered users and 109 guests