Search code examples
javainheritanceumlabstract-class

How do I strictly implement the given UML using Java?


I am trying to implement the UML below using Java:

enter image description here

I have successfully implemented every instruction except one which is:

Theater class:

  • Override the showArea method and display the size multiply by 12.

I am new to UML and from my understanding, I am not allowed to create constructors in any of the classes. This is confusing since I don't know where I can define the size for showArea.

Below is the working code I have right now.

Place Class

public abstract class Place {
    private String placeName;
    private int capacity;
    private String placeDescription;
    private int workingHours;


    public abstract void showEvents();
}

Building Class

public abstract class Building extends Place{
    
    public abstract void showArea();
}

Theater Class

public class Theater extends Building{
    @Override
    public void showArea() {
        System.out.println("Theater area : " );
    }

    @Override
    public void showEvents() {
        System.out.println("Events ready to be hosted !!");
    }
}

Main class

public class CodingtonDemo {
    public static void main(String[] args) {
        Theater theater = new Theater();
        theater.showArea();
        theater.showEvents();
    }
}

Expected result in console:

Theater area : 6000

Events ready to be hosted !!

My result so far:

Theater area : [No value yet]

Events ready to be hosted !!


Solution

  • The problem with your diagram

    Your diagram is a partial representation of your model:

    • The Place's properties placeName, placeDescription, capacity, workingHours are all private (-). This means that they are not accessivle for Building nor for Theater. Since you have no constructor and no setter, how can these properties be of any use? ANd since you have no public getter, how can these values be used in any showXxxx() operation of more specialized classes?

    • Since Building has no access to the private properties and has no other properties, how coud showArea() provide anything useful?

    • Finally, you're right about the overriding in Theatre. But the issues you've diagnosed for the missing size and its initialization are already true for Building.

    So you will never be able to achieve your expected results on the console if you strictly stick to your diagram and add no other implicit operations. I hope this will not be a shock to you. Here the UML specification quote that backs my statement:

    11.4.3.1: (...) A Class cannot access private Features of another Class, or protected Features on another Class that is not its ancestor.

    Minor syntactical issues, you could improve:

    • The italic notation is meant for class names to document that they are abstract. It is no longer officially defined for abstract operations, although many (I say this because I do it myself) still use this notation. Using italics for operations which have an implementation is therefore utterly ambiguous.

    • void is no standard UML type. An operation returning void is just indicated without return type.

    • showEvents in Theater desserves a pair of braces ().

    Complete your diagram

    You could add the missing getters and setters. And you could define a constructor as explained in the UML specifications:

    11.4.4: (...) A constructor is an Operation having a single return result parameter of the type of the owning Class, and marked with the standard stereotype «Create». The InstanceSpecification that is the supplier of the usage dependency represents the default value of the single return result parameter of a constructor Operation.

    This would look like:

    «Create» Place(...) : Place
    

    You have now the means to complete your diagram and achieve the expected results.

    Some more improvements may be desirable:

    • if Place implements no operation you should make it abstract, since it cannot be instantiated.
    • you may make the overriding explicit using the {redefines ...} in the diagram. But this is not necessary.
    • I guess that there is a missing relation to an Event class or interface, considering showEvents() and no other Events are in sight...

    Reconsider your design

    Is a theater a building? In my town, there is a building that hosts a mall AND a theater. So my advice would be to prefer composition over inheritance.