Java Interface Extend

Introduction

One interface can inherit another via the keyword extends.

When a class implements the extending interface, it must provide implementations for all methods required.

// One interface an extend another.
interface A {/*from  www  .ja v  a 2s.co  m*/
  void meth1();
  void meth2();
}

// B now includes meth1() and meth2() -- it adds meth3().
interface B extends A {
  void meth3();
}

// This class must implement all of A and B
class MyClass implements B {
  public void meth1() {
    System.out.println("Implement meth1().");
  }

  public void meth2() {
    System.out.println("Implement meth2().");
  }

  public void meth3() {
    System.out.println("Implement meth3().");
  }
}

public class Main {
  public static void main(String arg[]) {
    MyClass ob = new MyClass();

    ob.meth1();
    ob.meth2();
    ob.meth3();
  }
}



PreviousNext

Related