• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Interface question

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from Practice Exam Q7: SCJP Exam for J2SE 5 Platform Paul Sanghera

Consider the following source file:


Which of the following code lines insered 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();}

Answer: A+C

I put A, I don't see how C can be correct when the saySomething() method in Animal is implicitly public and here we'd be setting default access levels???
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can make the overriding method (public void graze())
less restrictive than the overridden method(void graze()) , so C should be correct.
 
Paul Stat
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kavita Tipnis wrote:You can make the overriding method (public void graze())
less restrictive than the overridden method(void graze()) , so C should be correct.



Yeah but it's not though is it because Pasture is extending Animal, therefore inheriting saySomething, which is implicitly public, therefore we can't override it and make the access MORE restrictive. So C must be wrong

And besides the public void graze(){} in interfaces IS the same as the one in Pasture as it's implicitly public (and abstract ofcourse)
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

You are right,You cant reduce the access in the implemented class
 
Kavita Tipnis
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Paul is right, I just looked it up to refresh interfaces and a compile of the program throws a clear exception
(trying to assign weaker access privileges to saySomething())
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya even i tried the code....of option C.
please tell me guys whats the difference between "public" and default.....
i just want to confirm
 
Paul Stat
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
himanshu it's quite simple really, interfaces methods are always implicitly public and abstract, so these two declaration of the animal interface are exactly the same



So in the first example even though we haven't declared it to be public + abstract IT IS!

Now when we have a class that implements this interface as



It may appear we've fulifilled the contract, BUT we're giving the saySomething method default privileges, which is MORE restrictive. The same is true when we extend a class and override it's methods, we can't make the overriding methods MORE restrictive

HTH
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you got it! option c is another interface so its methods are implicitly public and abstract! so no need for overriding!

 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Let me present the counter argument...I havent tried anything yet, I agree the overrideeen methods should not be more restritive.. but did you see interface itself is defined as "default" , so no point in implicitly public .. So C still holds good !!
 
Sarath Koiloth Ramath
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Balu,

if i am coorect interface can hold only public access specifer...
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sarath,

Yes , it holds methods which are implictly public.


Edit : Question misunderstood
 
Sarath Koiloth Ramath
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Balu ,

you are saying interface is declared as default access.So we can use default in the implemented class ?
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sorry dint get you... Sarath , i agree to the fact overridden method in implemented class cannot be more restrictive.( default than public) , well the argument is if interface itself is default what is the use of implicity public method other than to provide such overriding restriction..
 
Sarath Koiloth Ramath
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Balu,

I mean to say in a an interface, by default all declaration are public.

eg, if it is a variable , public static final int i=0;

and in case of a method it is, public abstact void test();

In a class all the declarations are "default" ie it is more restrictive than public which is not acceptable for an implemetation from an interface..

that is what i mean to say.

Please advice
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yup your point is right !! but i got a valid point too.. this argument reaches no where..

thanks Sarath.
 
Sarath Koiloth Ramath
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats right. We need to raise this point to sun consortium
 
expectation is the root of all heartache - shakespeare. tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic