0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

same name method that is default method of interface and class method

Last updated at Posted at 2023-01-11

default methodとclass method、同じ名前で定義し
implements, extendsすると、class methodが優先となる。
default methodは消失

B.methodがprivateの場合、compileはA.methodが使われてnormal end.
実行時にIllegalAccessError

interface A {
    default void method() {
        System.out.println("aaaaA");
    }
}
class B{
    public void method(){
        System.out.println("aaaaB");       
    }
}
class C extends B implements A{}
class D implements A{}
public class Outer {
  public static void main(String[] args) {
      Method m[]  = new C().getClass().getMethods();
      for(Method mc:m) {
          System.out.println(mc);
      }
      System.out.println("----------------------");

      m = new D().getClass().getMethods();
      for(Method mc:m) {
          System.out.println(mc);
      }
  }
}
public void com.mycompany.mavenproject1.B.method()
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
----------------------
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
public default void com.mycompany.mavenproject1.A.method()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?