Created
October 1, 2016 17:12
-
-
Save MykolaBova/6d81358bff92b5397a015406c33de300 to your computer and use it in GitHub Desktop.
Protected.java
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 Protected { | |
public static void main(String[] args) throws CloneNotSupportedException { | |
Car myCar1 = new Car(12,125); | |
System.out.println(myCar1); | |
// int i = myCar1.speedMax; | |
} | |
} | |
class Car implements Cloneable{ | |
protected int speedMax = 100; | |
private long id; | |
public Car(long id, int speedMax) { | |
this.id = id; | |
this.speedMax = speedMax; | |
} | |
public Car() { | |
} | |
@Override | |
public int hashCode() { | |
int result = (int) (id ^ (id >>> 32)); | |
result = 31 * result + speedMax; | |
return result; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if(this == o) return true; | |
if(o == null || getClass() != o.getClass()) return false; | |
Car car = (Car) o; | |
if(id != car.id) return false; | |
return true; | |
} | |
public int getSpeedMax() { | |
return speedMax; | |
} | |
@Override | |
public String toString() { | |
return "My name is car." + "speedMax = " + speedMax + "id = " + id; | |
} | |
public Car getNewCar() { | |
return new Car(); | |
} | |
protected void move() { | |
System.out.println("Car move."); | |
} | |
@Override | |
public Object clone() throws CloneNotSupportedException { | |
return super.clone(); | |
} | |
} | |
class Truck extends Car { | |
@Override | |
protected void move() { | |
System.out.println("Truck move."); | |
} | |
} | |
class SportCar extends Car { | |
protected int speedMax = 300; | |
public SportCar(long id, int speedMax) { | |
super(id, speedMax); | |
} | |
public SportCar() { | |
} | |
public int getSpeedMax() { | |
return speedMax; | |
} | |
@Override | |
public String toString() { | |
return super.toString() + "Sport car."; | |
} | |
public void testSuper() { | |
System.out.println(super.speedMax); | |
System.out.println(this.speedMax); | |
} | |
@Override | |
public SportCar getNewCar() { | |
return new SportCar(); | |
} | |
@Override | |
public void move() { | |
System.out.println("Sport car move."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment