#438 Colored Point in a Parent Constructor

Companies:
MoySklad
Themes:
JavaOOPCode Review

What will be printed, assuming the code compiles?

public static class Point {
    private final int x, y;
    private final String name;

    Point(int x, int y) {
        this.x = x;
        this.y = y;
        name = makeName();
    }

    protected String makeName() {
        return "[" + x + "," + y + "]";
    }

    public final String toString() {
        return name;
    }
}


public static class ColorPoint extends Point {
    private final String color;

    ColorPoint(int x, int y, String color) {
        super(x, y);
        this.color = color;
    }

    @Override
    protected String makeName() {
        return super.makeName() + ":" + color;
    }
}


public static void main(String[] args) {
    System.out.println(new ColorPoint(4, 2, "purple"));
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in