Skip to content

Commit ebe6b4e

Browse files
author
Pietro Di Bello
committed
add example of ovverriding and calling super on a overridden method
1 parent 8c0a4dd commit ebe6b4e

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package it.bemyeye.learn.subclass.override.example;
2+
3+
class Car {
4+
int cylinders;
5+
int currentSpeed;
6+
7+
public Car(int cylinders) {
8+
this.cylinders = cylinders;
9+
this.currentSpeed = 0;
10+
}
11+
12+
void accelerate(int speedGain) {
13+
currentSpeed += speedGain;
14+
}
15+
16+
public Double calcolaBollo() {
17+
return cylinders * 0.75;
18+
}
19+
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package it.bemyeye.learn.subclass.override.example;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class CarTest {
8+
9+
@Test
10+
public void calcolaBolloDipendeDallaCilindrata() {
11+
Car car = new Car(1000);
12+
assertEquals(750.0, car.calcolaBollo(), 0);
13+
14+
Suv suv = new Suv(1000);
15+
assertEquals(1500.0, suv.calcolaBollo(), 0);
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package it.bemyeye.learn.subclass.override.example;
2+
3+
class Suv extends Car {
4+
5+
public Suv(int cylinders) {
6+
super(cylinders);
7+
}
8+
9+
@Override
10+
public Double calcolaBollo() {
11+
return super.calcolaBollo() * 2;
12+
}
13+
}

0 commit comments

Comments
 (0)