• 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

Interface implementation

 
Ranch Hand
Posts: 220
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Questions is taken from SCJP Exam for J2SE 5 from Paul Sanghera


Consider the following source file:
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();}



I thought the answer is only A but the answer sheet says that A and C.

But, I thought that if we make saySomething default access in the subclass it is not allowed am I wrong because interface methods are public ?

we cannot convert public to default access, is it a typo like that ?
what do you think ?

 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yep, you are right...
 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Anut ,

The answers are A and C. If you observe the interface method 'saySomething' is never public in Animal.

And C is also an answer because Pasture is inheriting from 'saySomething' from Animal and adding a new method 'graze' to implement.And cow is implementing the both methods . Making graze() public is always allowed , as the access modifier of the method that is being implemented in the concrete class should same or less restrictive.
 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But interface methods are implicitly public so, you can't override it with default access (more restrictive), otherwise it gives error
Correct example should be like that
with public modifier.


interface Animal {
void saySomething();
}

class farm {
void setName(String name) {
};
}

interface Pasture extends Animal {
void graze();
}

public class Cow implements Pasture {
public void graze() {
}

public void saySomething() {
}
}
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the question the answer would be A only.
Try to compile this code it'll give an error of "Cannot reduce the visibilty of the inherited method".
[ October 20, 2008: Message edited by: Vipul Prajapati ]
 
Hang a left on main. Then read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic