About Question enthuware.ocajp.i.v7.2.1127 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

About Question enthuware.ocajp.i.v7.2.1127 :

Post by ETS User »

class and interface both are public here.how it can be possible in source file ?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post 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.
If you like our products and services, please help us by posting your review here.

Javanaut

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post 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. :ugeek:

declan_
Posts: 1
Joined: Wed Dec 11, 2013 4:05 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post by declan_ »

It's a little ambiguous since the question does not indicate that the files are in the same package.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post 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.
If you like our products and services, please help us by posting your review here.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post 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.
If you like our products and services, please help us by posting your review here.

sarakh
Posts: 23
Joined: Fri Jun 20, 2014 3:12 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post 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.
If you like our products and services, please help us by posting your review here.

javinocente
Posts: 1
Joined: Tue Aug 19, 2014 7:16 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post by admin »

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.

evafang2008
Posts: 9
Joined: Fri Jun 26, 2015 11:15 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post by admin »

What happened when you tried it out?
If you like our products and services, please help us by posting your review here.

evafang2008
Posts: 9
Joined: Fri Jun 26, 2015 11:15 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post 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.
If you like our products and services, please help us by posting your review here.

SeoaneR
Posts: 8
Joined: Tue Nov 07, 2017 8:23 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post 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
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]

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post by admin »

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.

Sai Krishna Datt
Posts: 2
Joined: Sat May 05, 2018 10:38 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post by admin »

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.

lolkin4777
Posts: 1
Joined: Thu Jun 18, 2020 1:13 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post by admin »

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.

Leonid
Posts: 9
Joined: Mon Jun 28, 2021 4:50 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post 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!
If you like our products and services, please help us by posting your review here.

Leonid
Posts: 9
Joined: Mon Jun 28, 2021 4:50 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1127 :

Post by Leonid »

Oh yes, right, not in different packages. I imagined it was the same. But I remembered now. Thank you.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 40 guests