jeudi 16 novembre 2017

Java how a class A can have multiple potential extends

Hello i have a problematic about inheritance.

Let's have this three :

           A
         /   \
        /     \
       B       C
       \       /
        \     /
           ?
           |
           D


    abstract class A {
        public abstract void print();
        public abstract int getInt();
    }

    abstract class B extends A {
        @Override
        public int getInt() {
          return 5;
        }
    }

    abstract class C extends A {
        @Override
        public int getInt() {
           return 1;
        }
    }


    class D extends B || C {
        @Override
        public void print() {
         console.log(this.getInt());
       }
    }

I want add a class D who can herit from B or C.

And when i create a new D(), i could choose the parent. I don't want use multiple inheritance (well java can't do it too), i would like to use a way to add specific comportment on a class by changing the implementation of his parent.

In fact D should herit from A but with the special implementation of B or C that i could choose at runtime. We can consider that B and C doesn't allow to implement other method than A one.

is that possible ?

Aucun commentaire:

Enregistrer un commentaire