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

question about interfaces

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,
i missed another one in doing some mock exams..

Question 6.

An Interface can never be private or protected.

Answers

True
False


i answered true, the exam says proper answer was false....

i got a feeling that exam is incorrect.... can anyone confirm?

regards
marco
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi marco,
an interface is always public abstract( you don't have to write it, its redundant )...but if you write an interface with another access modifier it will cause an compiler error.
arno
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arno Reper:

an interface is always public abstract( you don't have to write it, its redundant )



Is that necessarily true? Can't you have a package friendly interface?

If I write something like:



Couldn't be used outside the package com.nowhere.test but would be a valid interface definition.
 
Arno Reper
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you mean a package friendly interface?
package com.whatever.test;
interface MyInterface{}
is ok...
do you mean only useable in the package's class?
arno
 
James Ridley
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arno Reper:
How do you mean a package friendly interface?
package com.whatever.test;
interface MyInterface{}
is ok...
do you mean only useable in the package's class?
arno


Yeah, sorry should have clarified that. I was just trying to ensure I understood access levels on an interface. All the methods will be public by default, but the interface itself can have default package level access as opposed always being public.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by M Mistroni:

i missed another one in doing some mock exams..

Question 6.

An Interface can never be private or protected.



i answered true, the exam says proper answer was false....

i got a feeling that exam is incorrect.... can anyone confirm?



An package level interface can only public or dafault
but a nested interface can have all the access modifiers
interface A
{
}
class Outer
{protected interface B{}
private interface C{}
}
the above code will compile ok
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

All the rules for access level modifiers that are applicable to classes are applicable to interfaces also.
So any outer interface will be either default or public and nested interface can be private/protected/public!

But the mock test question was confusing for sure!

Abhishek
 
Hey! You're stepping on my hand! Help me tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic