Domanda

public class A{
    public A(int num){ num += 2;}
}

public class B Extends A{
    public B(int num){ super(num);}
}

public class C Extends B{
    public C(int num){ super(num);}
}

How would something like this look as a UML class diagram? normally if only one class inherited from another, then I would use generalization. How would I take this on? A sample diagram would be great.

È stato utile?

Soluzione

  • In UML, you don't need to repeat the same constructor in the children. They have it from the parent by default. That needn't be the same in all languages, though. So, you have these children constructors in Java, but you needn't show them in UML. But you can do it, of course.
  • The body of A() is senseless. You are changing the input parameter, and this has no consequences. I think, you meant:

 public class A{
    int num;
    public A(int num){ 
        this.num = num+2;
    }
 }

Anyway, this change is invisible on the class diagram

enter image description here

A class can easily have even two children, not a problem, in any language. The same for several generation generalization. In UML you can even have two parents of a child. But only few languages support this. Java is not in their number.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top