This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Interfaces and overriding

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.....
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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){}
}
}
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic