• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Circular dependency between parent class and child class

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what's wrong in this code? I really didn't understand what output expecting from this code.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you all... my doubt is cleared.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic