-1
interface I1 {
    int i = 10;
    void add();
    void sub();
    void del();
}
interface I2 {
    void disp();
}
class A implements I1,I2 {
    void add(){...}
    void sub(){...}
    void del(){...}
    void disp(){
        System.out.println(i);
    }
} 

This code is giving compile time error. I don't know what is the wrong with this code.

anirban karak
  • 732
  • 7
  • 20

6 Answers6

9

Implicitly, I1.add() et al are public. Therefore in your class you must also make them public:

public void add(){...}
public void sub(){...}
public void del(){...}
public void disp(){
    System.out.printf(i);
}

Otherwise you'd get

Cannot reduce the visibility of the inherited method from test.I1

Also, the call to printf() needs to change to:

System.out.printf("%d", i);

(The first argument is a format string.)

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 2
    Sadly, OP couldn't get this since he/she never read the compiler error messages =\ – Luiggi Mendoza Jul 18 '13 at 05:47
  • and about `i` that _i_ just mentioned in my answer – jmj Jul 18 '13 at 05:48
  • @JigarJoshi: I don't understand why you'd have to explicitly qualify `i` with `I1`. My compiler is happy with just `i` (and I don't see why it wouldn't be). – NPE Jul 18 '13 at 05:50
4

can't narrow down the visibility as @NPE mentioned and

i is public static final by default so access it by

I1.i
jmj
  • 237,923
  • 42
  • 401
  • 438
  • I still don't understand why you'd have to use the fully-qualified name. My compiler is happy with plain `i`, and I don't see why it wouldn't be. – NPE Jul 18 '13 at 06:07
  • to avoid confusion (it will compile fine eitherway) – jmj Jul 18 '13 at 06:35
2

All methods in an interface default to public.

See Java Language Specification 6.6.1 which states

All members of interfaces are implicitly public.

And you can't reduce the visibility of the methods when overriding them. You need to make them public.

Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
0

you cant use printf without using the format.

System.out.printf("%i", i);

would most likely allow you to do that, but without the format, it might not always work.

user2277872
  • 2,963
  • 1
  • 21
  • 22
0

Adding to Jigar Joshi Answer,

Why are interface variables static and final by default?

If you are using JDK above 1.4,

You can use static import; e.g.

  import static packagename.I1.*;

http://javarevisited.blogspot.in/2012/10/what-is-static-import-in-java-5-example-tutorial.html

Community
  • 1
  • 1
Shashi
  • 12,487
  • 17
  • 65
  • 111
  • 4
    If you are not using a JDK above 1.4, then I'd like to welcome you to the [21st century...](http://en.wikipedia.org/wiki/Java_version_history#J2SE_1.4_.28February_6.2C_2002.29) – jahroy Jul 18 '13 at 05:50
0

You can not reduce the visibility of inherited method. Inside interface, all variables and methods are by default public.

interface I1 {
    int i = 10;
    void add();
    void sub();
    void del();
}

interface I2 {
    void disp();
}

public class B implements I1,I2 {
    public void add(){}
    public void sub(){}
    public void del(){}
    public void disp(){
         System.out.println(i);
    }
}
Sufian
  • 6,405
  • 16
  • 66
  • 120
Ekanta Swain
  • 473
  • 2
  • 13
  • 28