| Author |
Interface question - Dan Chrisholm.
|
Tommaso Nuccio
Ranch Hand
Joined: Dec 11, 2006
Posts: 66
|
|
Hiho, on Dan Chrisholm's mock exam on interfaces: http://www.danchisholm.net/oct1/mybook/chapter10/exam1.html in question 16: interface A {void m1();} // 1 class B implements A {public void m1() {}} // 2 class C implements A {protected void m1() {}} // 3 class D implements A {private void m1() {}} // 4 class E implements A {void m1() {}} // 5 Compile-time errors are generated at which lines? a. 1 b. 2 c. 3 d. 4 e. 5 The answer is c,d,e. But I think, that line 2 also generates an error, as the curly brackets mean that one is implementing the method. I think that I am right, but in case I am not, can anyone point me to the hidden problem in that question? Thanks in advance, Tommaso
|
Ciao,<br /> Tommaso<br /> <br />~*~*~*~<br />There are 10 types of people, those who understand binary and those who don't.
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
Originally posted by Tommaso Nuccio: Hiho, on Dan Chrisholm's mock exam on interfaces: http://www.danchisholm.net/oct1/mybook/chapter10/exam1.html in question 16: interface A {void m1();} // 1 class B implements A {public void m1() {}} // 2 class C implements A {protected void m1() {}} // 3 class D implements A {private void m1() {}} // 4 class E implements A {void m1() {}} // 5 Compile-time errors are generated at which lines? a. 1 b. 2 c. 3 d. 4 e. 5 The answer is c,d,e. But I think, that line 2 also generates an error, as the curly brackets mean that one is implementing the method. I think that I am right, but in case I am not, can anyone point me to the hidden problem in that question? Thanks in advance, Tommaso
The reason that lines 3, 4, and 5 produce compile-time errors is that when you override a method, you can't make the method more private. Interface methods are automatically public. [ March 19, 2007: Message edited by: Keith Lynn ]
|
 |
Tommaso Nuccio
Ranch Hand
Joined: Dec 11, 2006
Posts: 66
|
|
Ahhhhhhh! Well, shoot... It says "class B implements A". Then of course it is clear. Thank you, Keith.
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
on Dan Chrisholm's mock exam on interfaces: http://www.danchisholm.net/oct1/mybook/chapter10/exam1.html in question 16: interface A {void m1();} // 1 class B implements A {public void m1() {}} // 2 class C implements A {protected void m1() {}} // 3 class D implements A {private void m1() {}} // 4 class E implements A {void m1() {}} // 5 Compile-time errors are generated at which lines? a. 1 b. 2 c. 3 d. 4 e. 5 The answer is c,d,e. But I think, that line 2 also generates an error, as the curly brackets mean that one is implementing the method. <br><br> In case of extrending abstract class, you can override a method with protected access if the abstract class is having with default access, and override it as public access if abstract class is having it with the protected access. But you can't do this in case of interfaces, Becauae as you should know, all the methods of an interface are implicitely public and abstract. So public can't be overridden with less access visibility. public(LY) accessible method can only be overridden using public access modifier, because the super class promises to be so, therefore the subclass can't make it less visible (breaking the contract). cmbhatt
|
cmbhatt
|
 |
 |
|
|
subject: Interface question - Dan Chrisholm.
|
|
|