Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

question about interfaces  RSS feed

 
M Mistroni
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • 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
 
Arno Reper
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • 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
 
James Ridley
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Changchun Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • 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
 
Abhishek Asthana
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!