简体   繁体   中英

How to prevent redundant code for interface

I've got an Interface B that extends Interface A.

Now I want a Class Bravo which implements Interface B.

As I have a Class Alpha which implements Interface A, is it possible just to declare

class Bravo extends Alpha implements B

and save the implementation of A's Methods due to the facet that Bravo inherits them?

Works fine for me:

interface A {
    public void foo();
}

interface B extends A {
    public void bar();
}

class Alpha implements A {
    public void foo() {
        System.out.println("foo");
    }
}

class Bravo extends Alpha implements B {   
    public void bar() {
        System.out.println("bar");  
    }   
}

class Test {

    public static void main(String[] args) {
        Bravo bravo = new Bravo();
        bravo.foo();
        bravo.bar();
    }
}

是的,可以通过继承实现

Thats perfectly possible. Effectively, Bravo will implement A and B and use Alpha 's implementation of A as its own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM