File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access Modifier

 
b bajwa
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abstract class ABQuestion
{
abstract void someMethod() ;
}

class ABCQuestion extends ABQuestion{
void someMethod()
{
System.out.println("Inside someemethod");
}
}
Above two classes i declared in seprate file, they are working fine.
If i change class ABQuestion to following interface, then class ABCQuestion give compilation error. Is there are different rules for interface access modifier and class? Any one explain the rule
interface ABQuestion
{
abstract void someMethod() ;
}
Thanks in advance
 
Jonatan Samoocha
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All methods declared in an interface are implicitly abstract. It is not accepted if you provide that keyword yourself.
Removing the "abstract" keyword from your interface method will make your code compile.
Jonatan
 
Jonatan Samoocha
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, I forgot one thing: another possible cause of compilation error is that your class should be defined as follows:

Regards,
Jonatan
 
b bajwa
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jonatan,
I did whatever you said, but still it give problem for access modifier. The only modifier we can used for this interface is public. If i give public acess modifier in ABCQuestion class, problem goes awaay. Why rules are different for interface?
Thanks
Balkar
 
kinnu jemmy
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi jonatan,
even if the abstract keyword is not needed before the method declaration,if we add it, it won't cause compiletime error.
hi bajwa,
methods inside interface are implicitly public.so in order to implement the method in we must use an access modifier having same or more access than the method in the interface.default access modifier has less access than public.
In the former case,default access modifier is used for the method in class ABQuestion.so while overriding we can use default, public or protected modifier.that is why compilation gives no error.
hope it helps
correct me if i am wrong.
kinnu
 
b bajwa
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi kinnu,
The default access modifier for interface is public , even if we do not provide any access modifier. But for class default access modifier is friendly. Am i correct?
 
kinnu jemmy
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi bajwa,
yes. you are correct.
that is why while overriding methods in class,u don't need to add
any modifier.but in order to implement methods in interface u have to specify the modifier as public.
regards,
kinnu
 
b bajwa
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kinuu
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic