Created
July 17, 2015 06:36
-
-
Save alexpana/b797c8da5c4f1ea9cd8a to your computer and use it in GitHub Desktop.
java puzzlers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point { | |
private final int x, y; | |
private final String name; // Cached at construction time | |
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 class ColorPoint extends Point { | |
private final String color; | |
ColorPoint(int x, int y, String color) { | |
super(x, y); | |
this.color = color; | |
} | |
protected String makeName() { | |
return super.makeName() + ":" + color; | |
} | |
public static void main(String[] args) { | |
System.out.println(new ColorPoint(4, 2, "purple")); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment