Do not judge strictly, a couple of days ago I decided to suddenly learn programming and downloaded a book for children on Java. I do this task: I create a class
public class Car { int distance=0; public void start() { System.out.println("Я поехал!"); } public void stop() { System.out.println("Я остановился!"); } public int drive(int howlong){ distance=howlong*60; System.out.println("Проехано уже " + distance + " м"); return distance; } } I create a class that will run.
public class CarOwner { public static void main(String[] args) { Car myCar = new Car(); myCar.start(); myCar.drive(3); myCar.stop(); } } At startup, the console issues
I went!
Already 180 meters driven
I stopped!
Next, create a subclass
public class BondCar extends Car { public int drive(int howlong){ return distance=howlong*180; } } Create class to manage subclass
public class CarOwnerBond { public static void main(String[] args) { BondCar myBondCar = new BondCar(); myBondCar.start(); myBondCar.drive(3); myBondCar.stop(); } } At startup, the console issues
I went! I stopped!
Why doesn't override work?