Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

java - Cyclic inheritance and interfaces - class A can't implement class B interface while class B implements A interface

I have:

public class A implements BListener {
    public interface AListener {}
}

public class B implements AListener {
    public interface BListener {}
}

So, if I understand correctly the cyclic inheritance happens because:

The compiler goes to A and says "hey, A implements BListener, let's go find BListener!"

Then when it tries to find BListener, it eventually gets to B, which it says:

"Hey, BListener, needed for A is inside B! BUT WAIT! B needs AListener! Let's go find AListener!"

And then it gets to A, repeat. Did I get this right?

By the way, this compilation error happened to me on Android development.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It may help to draw it out.

              >A 
is part of  /         inherits
                   V             
   AListener        BListener
           ^
  inherits       /    is part of 
               B<

A lovely circle. You can't create one of them without the others already existing.

Is the compiler a squirrel with ADHD high on coffee chasing it's own tail?

Nope because a the squirrel would not stop (until the caffeine ran out). The compiler looks for this and then gives up.

Note: Eclipse has a bug which allows this setup.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...