InterfaceClash.java


import java.io.*;

public class InterfaceClash {

   public static void main (String[] args)throws IOException{
      Strange s = new Strange();
      
      System.out.println(I1.v1 + " " + I1.v2);
      System.out.println(I2.v1 + " " + I2.v2);
      System.out.println(s.s   + " " + s.s1 );
      s.Strange();
      
      System.in.read();
   }
}

class Strange implements I1, I2{
   int v1, v2;
   int s;
   int s1;
   
   // Two interface var name clash can be solved by qualify them
   Strange() { s = I1.v1 + I2.v2; s1 = v1 + v2;}
   
   // This is not a constructor!
   void Strange() {System.out.println("I'm Strange and Strange!");}
   
   // Attempt to reduce access level of member void I2.f()
   // void f(){} // not compilable
   
   // cannot override non-static method I1.g() to static
   // public static int g(){}  // not compilable

   public void f()     {}
   public int  g()     {return 1;}
   public void h()     {}
   public int  h(int n){return n;}
}

// Anything defined in interfaces is implicitly public
// Any variable is implicitly static final
// Any method is never static
// 
interface I1{
   int v1 = 123;
   int v2 = 456;
   
   int  g();
   void h();
}

interface I2{
   int v1 = 12;
   int v2 = 45;
   
   void f();
   // Same g(); in I1, no problems
   int  g();
   // different siganiture in I1, no problems
   int  h(int n);
   
   // this h() has same name and signature as in I1, 
   // but differs on return type
   // This one is OK in itself
   // it will cause all kinds of problems in
   // class Strange implements both I1 and I2
   // int h(); // same name and signature as in I1
}

last updated: 10-14-1999
Copyright © 1999 - 2003 Roseanne Zhang, All Rights Reserved
[ Java certification page] [ SCJP FAQ] [ JavaChina]