javainheritanceumlobject-diagram

UML Object Diagram: Parent or Child


Let's say I have the following class :

public class ParentClass{}

And one of its children classes is :

public class ChildClass extends ParentClass{}

Let's consider another class :

public class Foo{
    
    private ParentClass field;
    
    public Foo(){
        
        field = new ChildClass();
        
    }
    
    public static void main(String[] args){
        
        Foo foo = new Foo();
        
    }
    
}

When drawing the Object Diagram of foo I don't know if I have to write ChildClass :

enter image description here

Or ParentClass :

enter image description here


Solution

  • In your program field is an instance of ChildClass, the goal of the object diagram is to show instances with their effective type (and their attributes value if they have) => the right diagram to use is

    enter image description here


    [edit from remarks to my answer]

    It is also legal to use the second diagram, but that one hide the fact the instance is for sure an instance of ChildClass, so better to use the first diagram to indicate that.

    I forgot to mention in both your diagrams the line between the two instances does not correspond to the alone association between them, a right way to do for the first diagram is :

    enter image description here

    considering your instance of Foo is saved through a variable named foo it seems reasonable to name foo your instance of Foo