Created
October 1, 2016 16:26
-
-
Save MykolaBova/324a606cc917d377c79f763f3cfea406 to your computer and use it in GitHub Desktop.
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 Covariant { | |
public static void main(String[] args) { | |
SportCar car3 = new SportCar(789, 23); | |
car3.testSuper(); | |
} | |
} | |
class Car { | |
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."; | |
} | |
public Car getNewCar() { | |
return new Car(); | |
} | |
} | |
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment