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 Interfaces and overriding 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 "Interfaces and overriding" Watch "Interfaces and overriding" New topic
Author

Interfaces and overriding

Shafeeq Sheikh
Ranch Hand

Joined: May 12, 2000
Posts: 68
Hi Guys.....
Here's a piece of code I found on a mock exam.....
interface Int
{
void someMethod();
}

public class AQuestion implements Int{
void someMethod(){
System.out.println("from the implemented method");
}
public static void main(String args[]){
System.out.println("Before method call");
AQuestion obj = new AQuestion();
obj.someMethod();
}
}
When I compile it I get an error saying:
C:\java\bin>javac AQuestion.java
AQuestion.java:9: The method void someMethod() declared in class AQuestion cannot override the method of the same signature declared in interface Int. The access modifier is made more restrictive.
void someMethod(){
^
1 error
Now what I don't understand is the the method in interface Int has the default access modifier. I am trying to override it with another method in the class AQuestion, again with a default modifier..... Then why does the compiler complain about the access modifier being made more restrictive???
Any inputs would help.....
Thanks.....
Srini B
Greenhorn

Joined: Jun 22, 2000
Posts: 3
Hi!
The methods in an interface are public by default.
Now you are making the implementing the method as 'friendly', so you are restrictiong the access of the method.
Make the method 'public' in the implemented class, it will work.
Good Luck

[This message has been edited by Srini B (edited July 06, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Because when you are overriding a method. From a class or interface.
1. your access modiifier can NOT be more restrictive.
2. you can NOT throw a CHECKED exception not thrown by the
the overridden method.
3. if the overridden is syncronized. you do not have to make
you new method syncronized.
Pls correct me if i am wrong.
Hope this helps.
Monty
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by monty6:
Because when you are overriding a method. From a class or interface.
1. your access modiifier can NOT be more restrictive.
2. you can NOT throw a CHECKED exception not thrown by the the overridden method.
3. if the overridden is syncronized. you do not have to make your new method syncronized.
Pls correct me if i am wrong.
Hope this helps.
Monty

Shafeeq Sheikh
Ranch Hand

Joined: May 12, 2000
Posts: 68
Thanks Srini and Monty..... that helped.....
but a couple of more doubts.....
Can a method (declared in an interface) throw exceptions at all .
I tried this and it still gives me quite a few errors...
interface Int
{
void someMethod throws Exception{}
}

public class AQuestion implements Int{
public void someMethod() throws Exception{
System.out.println("from the implemented method");
}

public static void main(String args[]){
System.out.println("Before method call");
AQuestion obj = new AQuestion();
try{
obj.someMethod();
}
catch (Exception e){}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interfaces and overriding