• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interface methods

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am running following code...It is giving compilation error at Line 1
. Cannot reduce the visibility of inherited method from I1
. Cannot reduce the visibility of inherited method from I2

It compiled fine when i changed print in Test to public.
Could anyone please explain me..

public class Test implements I2 {
public static void main(String args[])
{
Test t = new Test();
t.print();
}
void print(){ //1
System.out.println(value);
}
}

interface I1 {
int value = 1;
void print();
}

interface I2 extends I1 {
int value =2;
void print();
}
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



void print(){ //1
System.out.println(value);
}




Interface methods are implicitly public and abstract.
So if you make your print method public - you should be alright.

Richard.
 
Enthuware Software Support
Posts: 4803
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key is in understanding the statement, "Cannot reduce the visibility of inherited method".

Let's say you have a class Person having a public method getAge(). You have another class say MortalityRiskCalculator, that has a method calculateRisk(Person p) :-


This above code calculates risk for any given Person and is only dependent on the person's age. However, in your business model, you could have several types of Person such as Manager, Worker, and Owner. All of these may extend from Person class thereby inheriting the getAge() method.

Now, what will happen if you override getAge() method in Manager class and make it private? It will break all the code that depends on Person class's getAge() method. When you wrote Person class, you promised that getAge() is accessible to all and based on that promise, someone wrote the MRCalculator class, which has no idea about what all kinds of Persons you have. If you make the getAge() method private in Manager class, you are breaking that promise because Manager IS-A Person.

Same thing happens with an Interface. As Richard mentioned, methods in an interface are public. Any class that implements an interface promises that all the inteface methods that it implements will be public. If you make any such method "less accessible than public", you are breaking that promise.

This fulfilment of contractual obligation is critical in developing component based applications.

You will see the same concept in action when figuring out which exceptions an overriding method (or a method that implements a method declared in an interface) is allowed to throw. Try it as an exercise As an hint, just think of what will happen to the existing users of the interface/superclass if you introduce a new exception in the throws clause of the implementing/overriding method.

HTH,
Paul.
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By saying that a class implements an interface. You're agreeing to the outside world that your
class has a certain behavior which is defined in the interface.

If the methods which your promise to implement in your class are not public then the outside world cannnot
use your methods, so that's why al the interface methods are always public.

They are also abstract because there is no implementation definied in the interface.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simple method overridden rule is "we can not narrow the scope of methods".
It means, if we have protected and default methods in super class, we can make it public in subclass.

In interface, all the methods are by default public and abstract.
And you are trying to make it default.
You are violating the overridden contract here.

That's why, you are getting error.

I think its enough to understand.
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yea, all interface methods are always public, whenever you implement an Interface, the methods have to be public.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic