About Question enthuware.ocajp.i.v7.2.1127 :
Moderator: admin
-
- 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: 10065
- 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: 10065
- 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: 10065
- 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: 10065
- 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: 10065
- 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: 10065
- 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 113 guests