1
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.

default method of interface

Last updated at Posted at 2023-01-10

default methodはextendsによりoverrideされる

interface A {
    default void method() {
        System.out.println("aaaa");
    }
}
interface B extends A {
}

 public class Outer implements B {
    @Override
    public void method() {
        System.out.println("aaaa2");
        B.super.method();
    }
  public static void main(String[] args) {
      new Outer().method();
  }
}
aaaa2
aaaa

interfaceがextendsするときも利用可能
default methodは、extendsしたinterfaceのdefault methodをoverrideするときにだけ参照できる模様。
Outer extends Bのため、B.super.method()はokだが、A.super.method()はng

interface A {
    default void method() {
        System.out.println("aaaaA");
    }
}
interface B extends A {
    @Override
    default void method() {
        System.out.println("aaaaB");
        A.super.method();
    }
}

 public class Outer implements B {
    @Override
    public void method() {
        System.out.println("aaaaOuter");
        B.super.method();
    }
  public static void main(String[] args) {
      new Outer().method();
  }
}
aaaaOuter
aaaaB
aaaaA
1
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
1
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?