• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question from Sun guoqiao mock exam2

 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am glad to hear that:P
 
dylan shen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic