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
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
posted
0
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
posted
0
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
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
posted
0
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
posted
0
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
posted
0
Thanks Kinuu
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.