File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Circular dependency between parent class and child class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Circular dependency between parent class and child class" Watch "Circular dependency between parent class and child class" New topic

Circular dependency between parent class and child class

Sumit Goel

Joined: Apr 10, 2010
Posts: 2
public class A {
public static void main(String [] args){
B test = new B();

public class B extends A {
public void doSomething(){
System.out.println("I am confused");

When I run the code, it works fine. It prints "I am confused"
I am a little curious about how will the 2 classes compile? A is the Super Class and has a reference to its Sub Class B. So the compiler needs Class B to compile Class A. And as B is the sub class, the complier would need the Class A to compile Class B
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 632

what's wrong in this code? I really didn't understand what output expecting from this code.

Life is easy because we write the source code.....
jammy chen

Joined: Aug 24, 2009
Posts: 25
This is exactly good question.

I think the java compiler should compile at first, of course when compile the code B test = new B() in main method, it will try to look for and doSomething() method within, althought has extended But it's nothing for this time because A has already found. you should know the compile progress is only try to compile java code to binary class file, it does not do any real execute code step, aslo when compile there have additional and necessary process is confirm the reference class should be exist and then compile class by class.

Java tutorial
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46393
If the two classes are in the same package, the compiler will have no difficulty finding them. In the unnamed package it is easy; if you have named packages you would write something like
javac -d . foo.*.java

But you have obviously seen already, that circular dependency is a weird design.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3028
This isn't really a circular dependency. See JLS 8.1.4:
A class C directly depends on a type T if T is mentioned in the extends or implements clause of C either as a superclass or superinterface, or as a qualifier of a superclass or superinterface name. A class C depends on a reference type T if any of the following conditions hold:
  • C directly depends on T.
  • C directly depends on an interface I that depends (§9.1.3) on T.
  • C directly depends on a class D that depends on T (using this definition recursively).

  • It is a compile-time error if a class depends on itself.

    For example:

    class Point extends ColoredPoint { int x, y; }
    class ColoredPoint extends Point { int color; }

    causes a compile-time error.
    If circularly declared classes are detected at run time, as classes are loaded (§12.2), then a ClassCircularityError is thrown.

    The key part of this, for our purposes here, is that dependency is defined in terms of types that appear in an extends or implements clause. It doesn't matter if a class appears elsewhere, such as the type of a field, or method or constructor calls. So applying this definition to the code above, class B depends on A (because it extends A). Class A does not depend on class B.

    As a practical matter, how does the compiler achieve this? Well, compilation can be a multi-pass thing. It's not necessary to completely compile one class before even looking at another class. Probably the compiler looks at extends clauses in both classes first, and figures out that B depends on A. Then it looks at all the method signatures in each and remembers them. Then it compiles A. When it sees the reference to B, that's not a problem - the compiler knows there is a class named B, and knows what methods you can call on a B, even though those methods haven't been fully compiled yet. It just inserts code that says to go find class B and execute certain methods in it. It doesn't need to know exactly what those methods do. This allows it to complete the compilation of A. Once done with that, it can compile B as well, no problem.
    Sumit Goel

    Joined: Apr 10, 2010
    Posts: 2
    Thank you all... my doubt is cleared.
    I agree. Here's the link:
    subject: Circular dependency between parent class and child class
    It's not a secret anymore!