File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Access Modifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Access Modifier" Watch "Access Modifier" New topic
Author

Access Modifier

b bajwa
Greenhorn

Joined: Oct 26, 2001
Posts: 12
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

Joined: Nov 20, 2001
Posts: 16
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

Joined: Nov 20, 2001
Posts: 16
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

Joined: Oct 26, 2001
Posts: 12
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

Joined: Oct 08, 2001
Posts: 18
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

Joined: Oct 26, 2001
Posts: 12
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

Joined: Oct 08, 2001
Posts: 18
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

Joined: Oct 26, 2001
Posts: 12
Thanks Kinuu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access Modifier