• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Implementing interfaces

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Folks,
I'm working with APress SCJP exam book.
In the final test I found one qeustion(No 7 in final test):


1. interface Animal {
2. void saySomething();
3. }
4. class farm {
5. void setName(String name){};
6. }
7. // insert code here
8. public class Cow implements Pasture {
9. public void graze() { }
10. void saySomething(){}
11.}
Which of the following code lines inserted independently at line 7 will make this source file compile?
A. interface Pasture {void graze();}
B. interface Pasture {void graze(){}}
C. interface Pasture extends Animal{void graze();}
D. interface Pasture extends Animal{void saySomething(){}}
E. interface Pasture implements Animal{void graze();}



What are your candidates?
B and D is wrong because you cannot implement a method in interface.
E is wrong because interface can extends not implement another interface.
A is OK.
How about C? It looks good but when we look carefully we can see that in Cow class the method saySomething is marked as a default.
This method is also declared in Animal interface, also without any word about public. But all methods, variables in interfaces are public.
So C is not correct because if Pasture implement Animal than the Cow class will not compile.
Please correct me if I'm wrong.

Thanks,
Tomek
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tomek Kaczmarek:
Folks,
I'm working with APress SCJP exam book.
In the final test I found one qeustion(No 7 in final test):


What are your candidates?
B and D is wrong because you cannot implement a method in interface.
E is wrong because interface can extends not implement another interface.
A is OK.
How about C? It looks good but when we look carefully we can see that in Cow class the method saySomething is marked as a default.
This method is also declared in Animal interface, also without any word about public. But all methods, variables in interfaces are public.
So C is not correct because if Pasture implement Animal than the Cow class will not compile.
Please correct me if I'm wrong.

Thanks,
Tomek



Yes, Tomek. Thats right. We cannot have the weaker access in class than in interface.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep the answer must be A.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are right
 
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic