Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface question - Dan Chrisholm.

 
Tommaso Nuccio
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhhhhhh!

Well, shoot...
It says "class B implements A".
Then of course it is clear.

Thank you, Keith.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic