Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private/Protected interfaces

 
Shafeeq Sheikh
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a question in Abhilash's exam that goes like this:
An Interface can never be private or protected.
True
False
The given answer is FALSE.....
But I wrote this program:
private interface Int
{
void someMethod();
}
public class AQuestion implements Int{
public void someMethod(){
System.out.println("from the implemented method");
}
}
and the compiler complains..... Even if I change the access modifier to protected (for the interface)..... it complains again....
Is the answer in his exam wrong or am I missing something???
Thanks in advance.....
 
Savithri Devaraj
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like a top level interface cannot be private or protected.
I modified your code as below and it works just fine
public class AQuestion {
private interface xint{
void someMethod();
}
class newc implements xint{
public void someMethod(){
System.out.println("from the implemented method");
}
}
public static void main(String args[]){
AQuestion.newc aq = new AQuestion().new newc();
aq.someMethod();
}
}
 
Savithri Devaraj
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where is Abhilash's exam? Anywhere online??
Savithri
 
Shafeeq Sheikh
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There you go:
http://www.angelfire.com/or/abhilash/Main.html
 
Shafeeq Sheikh
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There you go...
http://www.angelfire.com/or/abhilash/Main.html
 
chetan nain
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interfaces may only be declared public or package access(no access modifier).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic