wood burning stoves 2.0*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
Author

Circular dependency between parent class and child class

Sumit Goel
Greenhorn

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

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: 629

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
Greenhorn

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

I think the java compiler should compile A.java at first, of course when compile the code B test = new B() in main method, it will try to look for B.java and doSomething() method within B.java, althought B.java has extended A.java. 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
Sheriff

Joined: Oct 13, 2005
Posts: 37939
    
  22
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: 2986
    
    9
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
    Greenhorn

    Joined: Apr 10, 2010
    Posts: 2
    Thank you all... my doubt is cleared.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Circular dependency between parent class and child class
     
    Similar Threads
    fun with main() and abstract
    Please explain(about accessing members)!
    scjp 5 tough questions
    Casting urgently! Marcus Green Exam1
    interfaces and subclasses