-
UML to Java question
I have a UML that I have to convert to Java code. The only thing I cannot figure out is how to call method op2() in class B from classes C and D. If I create an object of class B, then the whole program goes into an infinite loop. And I cannot even make the method op2() a class method. Can anyone tell me where I need to correct the code?
http://img512.imageshack.us/img512/408/17721211.jpg
Code:
public class B extends A {
public static void main(String args[]) {
new B();
}
private C c;
private D d;
public B() {
System.out.println("B()");
c = new C(this); // 1:
d = new D(this); // 2:
c.op3(d); // 3:
d.op4(c); // 4:
}
public void op2() {
System.out.println("B.op2()");
op1(); // 3.1.1.1: and 4.1.1.1:
}
}
class A{
public void op1() {
System.out.println("A.op1()");
}
}
class C {
private B b;
private D d;
public C(B b) {
System.out.println("C()");
}
public void op3(D d) {
System.out.println("C.op3()");
d.op5();
}
public void op5() {
System.out.println("C.op5()");
b.op2(); //erroneous part
}
}
class D {
private B b;
private C c;
public D(B b) {
System.out.println("D()");
}
public void op4(C c) {
System.out.println("D.op4()");
c.op5();
}
public void op5() {
System.out.println("D.op5()");
b.op2(); //erroneous part
}
}
-
Re: UML to Java question
Hello,
What exactly are you trying to do in your classes. The op2() method is in class B. So, if you need to use this method in class C and class D then you will need to extend class C and class D with class B. I think this is the solution for it. Also, if you need some other way round then you can also you class B as an interface so that it can be called by class C and class D at the same time, that is multiple inheritance.
Page generated in 1,714,164,603.17041 seconds with 10 queries