wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question from Sun guoqiao mock exam2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question from Sun guoqiao mock exam2" Watch "question from Sun guoqiao mock exam2" New topic
Author

question from Sun guoqiao mock exam2

Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204



What is the output of trying to compile and run the following code?
(Select one correct answer)
-----------------------------------------------------------------------
public class Test015 implements Interface //1
{
public native void method1(); //2
protected void method2(){ } //3
}
interface Interface
{
void method1();
void method2();
}
-----------------------------------------------------------------------
A: The code does not compile due to line //1.
B: The code does not compile due to line //2.
C: The code does not compile due to line //3.
D: The code just compiles fine without error.

answer given is c but i feel it should also be answer b
as methods in interface are implicitly abstract and they are declared as native in line 2 .abstract and native do not go together.
Correct me if i am wrong
regards
mrunal


nss
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578


answer given is c but i feel it should also be answer b
as methods in interface are implicitly abstract and they are declared as native in line 2 .abstract and native do not go together.

Hi Mrunal/Neha ???
The correct answer is C because methods in interface are all public and line 3 declares it as protected.
As for B, yes all methods in an Interface are public and abstract, but that's why you have to provide implementation for them in the the class which imlements the interface. "native" is a keyword that instructs the compiler that implementation is provided as a native code, and not as a Java code. That is perfectly valid and does not make that mehod abstract in the class.
HTH,
- Manish

Yin Ming
Ranch Hand

Joined: Oct 12, 2001
Posts: 41
native means it implements it by a extern model. so it has no method body. it unlike the stract method which also has no body. so b is correct.
the override method cannt be more private than super's. in iterface, all the methods are public. it can only be implemented with public. so c is incorrect.
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
thanx Manish and Yin Ming for clearing my doubts.
There is a bit missprint in Yin Ming wordings but i understood what he is trying to explain.
Thanx once again.
Yin Ming
Ranch Hand

Joined: Oct 12, 2001
Posts: 41
I am glad to hear that:P
dylan shen
Greenhorn

Joined: Oct 20, 2001
Posts: 10
Originally posted by Manish Hatwalne:
Hi Mrunal/Neha ???
The correct answer is C because methods in interface are all public and line 3 declares it as protected.
As for B, yes all methods in an Interface are public and abstract, but that's why you have to provide implementation for them in the the class which imlements the interface. "native" is a keyword that instructs the compiler that implementation is provided as a native code, and not as a Java code. That is perfectly valid and does not make that mehod abstract in the class.
HTH,
- Manish

Dear Manish,
Since all methods are public for interface. Why the methods for interface are declared as default, not public?
Thanks

Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

Originally posted by dylan shen:
Since all methods are public for interface. Why the methods for interface are declared as default, not public?

Hi Dylan,
The methods in the interface are by default public, an explicit access modifier "public" is not required. In this example, absence of an access modifier does not make them package/default.
The class implementing this interface defines one of the methods as protected, and a method can not be overriden to be less public. Hence an error.
HTH,
- Manish
 
 
subject: question from Sun guoqiao mock exam2