fork download
  1. class Protected {
  2. public static void main(String[] args) throws CloneNotSupportedException {
  3.  
  4. Car myCar1 = new Car(12,125);
  5. System.out.println(myCar1);
  6.  
  7. // int i = myCar1.speedMax;
  8. }
  9. }
  10.  
  11. class Car implements Cloneable{
  12. protected int speedMax = 100;
  13. private long id;
  14.  
  15.  
  16. public Car(long id, int speedMax) {
  17. this.id = id;
  18. this.speedMax = speedMax;
  19. }
  20.  
  21. public Car() {
  22.  
  23. }
  24.  
  25. @Override
  26. public int hashCode() {
  27. int result = (int) (id ^ (id >>> 32));
  28. result = 31 * result + speedMax;
  29. return result;
  30. }
  31.  
  32. @Override
  33. public boolean equals(Object o) {
  34. if(this == o) return true;
  35. if(o == null || getClass() != o.getClass()) return false;
  36. Car car = (Car) o;
  37. if(id != car.id) return false;
  38. return true;
  39. }
  40. public int getSpeedMax() {
  41. return speedMax;
  42. }
  43.  
  44. @Override
  45. public String toString() {
  46. return "My name is car." + "speedMax = " + speedMax + "id = " + id;
  47. }
  48.  
  49. public Car getNewCar() {
  50. return new Car();
  51. }
  52.  
  53. protected void move() {
  54. System.out.println("Car move.");
  55. }
  56.  
  57. @Override
  58. public Object clone() throws CloneNotSupportedException {
  59. return super.clone();
  60. }
  61. }
  62.  
  63. class Truck extends Car {
  64. @Override
  65. protected void move() {
  66. System.out.println("Truck move.");
  67. }
  68. }
  69.  
  70. class SportCar extends Car {
  71. protected int speedMax = 300;
  72.  
  73. public SportCar(long id, int speedMax) {
  74. super(id, speedMax);
  75. }
  76.  
  77. public SportCar() {
  78.  
  79. }
  80.  
  81. public int getSpeedMax() {
  82. return speedMax;
  83. }
  84.  
  85. @Override
  86. public String toString() {
  87. return super.toString() + "Sport car.";
  88. }
  89.  
  90. public void testSuper() {
  91. System.out.println(super.speedMax);
  92. System.out.println(this.speedMax);
  93. }
  94.  
  95. @Override
  96. public SportCar getNewCar() {
  97. return new SportCar();
  98. }
  99.  
  100. @Override
  101. public void move() {
  102. System.out.println("Sport car move.");
  103. }
  104. }
Success #stdin #stdout 0.04s 711168KB
stdin
truck
stdout
My name is car.speedMax = 125id = 12