llamaro25
llamaro25

Reputation: 692

How to draw UML diagrams passing parameters through super constructor

I have a superclass called A and a subclass called B that inherits from A. The superclass's constructor looks like this:

A(String name, char displayChar, int hitPoints, Behaviour behaviour) 

{
 this.name = name;
 this.displayChar = displayChar;
 this.hitPoints = hitPoints
 addBehaviour(behaviour);
}

A has attributes of name, displayChar, hitPoints, behaviour and has a method that calls addBehaviour which adds the behaviour to the object.

The subclass, B's constructor looks like this:

B(String name) {super(name, char 'b', 10, new WalkBehaviour()); }

Now my question is, does subclass B have an attribute of WalkBehaviour?

How would the UML diagram look like for this scenario? I know B inherits from A and A has Behaviour but does B has WalkBehaviour in this case? Since B doesn't have an instance variable of type WalkBehaviour in its class but only passes WalkBehaviour through its superclass's constructor.

Upvotes: 2

Views: 2217

Answers (1)

qwerty_so
qwerty_so

Reputation: 36333

does subclass B have an attribute of WalkBehaviour?

No. There is none declared. The superclass will do something with that new object but obviously it's hidden in the mist of its implementation.

Inheritance is nothing that involves multiple object creation. Your B instance is just a single one which does have attributes and operations like its super class.

enter image description here

Thus, in a SD, you will see only one life line for B:

enter image description here

As you can see the B instance will just issue a self-call to the super class's constructor.

Note: as @AxelScheithauer pointed out in the comment the super class will invoke addBehavior which can (but must not) be shown in the SD:

enter image description here

Upvotes: 2

Related Questions