开发者

Interfaces, inheritance and subtyping

开发者 https://www.devze.com 2023-01-26 21:11 出处:网络
I have the following JAVA mess that is very unclear to me: a generic Interface with two generic Types A and B, such as AIter<A,B>

I have the following JAVA mess that is very unclear to me:

a generic Interface with two generic Types A and B, such as AIter<A,B>

another generic In开发者_如何学Cterface with an additional generic Type C, like this: BIter<A,B,C> extends AIter<A,B>

one generic class, ClassA<D>, which recursively implements the first Interface AIter as an inner class like this: ThisIter implements AIter<Something,ThisIter>

another generic class, ClassB<D,E>, which recursively implements the second interface BIter as an inner class like this: ThisIter implements BIter<Something,ThisIter,SomethingElse>

Both of the classes have a function called public ThisIter giveIter(); which should return an instance of the inner class (each of the inner classes has a similar function which also returns a new instance of itself, but with a different parameter).

Is it possible to implement ClassB as subtype of ClassA? (Given that the generic Types are the same, such as ClassA<MyString> and ClassB<MyString, somethingelse>)

I can't seem to make it happen, because the instances of ThisIter and therefor the return types of giveIter() are incompatible.

Generally asked, is an implementation of a subinterface (n)ever a subtype of an implementation of the interface itself?

IterA

public interface IterA<A,B> {}

IterB

public interface IterB<A,B,C> extends IterA<A,B> {}

ClassA:

public class ClassA<L> {
    Node root;

    public ClassA() {
        root= new Node();
    }

    protected class Node {
       /*...*/
    }

    protected class Edge {
        /*...*/
        L varL;
    }

    protected class ThisIter implements IterA<Edge,ThisIter> {
    /*...*/
    }
    public IterA<Edge,ThisIter> assoc() {
        return new ThisIter(root);
    }
}

ClassB:

public class ClassB<L,N> {
    Node root;

    public ClassB() {
        root= new Node();
    }

    protected class Node {
       /*...*/
       N varN;
    }

    protected class Edge {
        /*...*/
       L varL;
    }

    protected class ThisIter implements IterB<Edge,ThisIter,N> {
    /*...*/
    }
    public IterB<Edge,ThisIter,N> assoc() {
        return new ThisIter(root);
    }
}


We can't override inner classes. Read here about Can inner classes be overridden.

Q. Is it possible to implement ClassB as subtype of ClassA?

A. Yes, you need to choose a different name for your ClassB.getIter(). Otherwise, it will complain about overridden method has different return type or something. In fact, you are not intended to override that, but compiler has no way to know. Hence, you need to choose some other name for it.

Q. Generally asked, is an implementation of a subinterface (n)ever a subtype of an implementation of the interface itself?

A. No, it would be 2 different class hierarchies, in that case. One from top to bottom, interfaceA -> interfaceB -> classD, and the other interfaceA -> classC.


Reconsider your naming and code factoring, in particular do you really need a class called node and edge in each of your container classes?

Here is something compilable, resembling your code (as I understand the intention.)

    interface IterA<A,B> {}
    interface IterB<A,B,C> extends IterA<A,B> {}

    class ClassA<L> {
        NodeA root;
        public ClassA() { root= new NodeA(); }
        protected class NodeA { }
        protected class EdgeA { L varL; }
        protected class ThisIterA implements IterA<EdgeA, ThisIterA> { ThisIterA(NodeA root) { } }

        public IterA<? extends EdgeA, ? extends IterA> assoc() { return new ThisIterA(root); }
    }

    class ClassB<L,N> extends ClassA<L> {
        NodeB root;
        public ClassB() { root= new NodeB(); }
        protected class NodeB extends NodeA { N varN; }
        protected class EdgeB extends EdgeA { L varL; }
        protected class ThisIterB<EA extends EdgeA, TIA extends IterB> implements IterB<EA, TIA, N> { ThisIterB(NodeB root) { } }

        public IterB<EdgeB, IterB, N> assoc() { return new ThisIterB<EdgeB, IterB>(root); }
    }

It's getting late here, so that's the last of me. Good luck and good night!


Not sure if this helps, but the following compiles

interface AIter<A,B>{}
interface BIter<A,B,C,D extends A,E extends B > extends AIter<A,B>{}

class AClass{
    protected class Node {}
    protected class Edge {}

    public class ThisIter implements AIter<Node,Edge>{};

    public ThisIter getIter(){
        return new ThisIter();
    }
}

class BClass<N> extends AClass{
    protected class Node extends AClass.Node{}
    protected class Edge extends AClass.Edge{}

    public class ThisIter extends AClass.ThisIter implements BIter<AClass.Node,AClass.Edge, N, Node, Edge>{};

    @Override
    public ThisIter getIter(){
        return new ThisIter();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号