- INDENT THE CODE! AND FIX THE FORMATTING ISSUES!
lass Bike { final void run() { System.out.println("running"); } } class Honda extends Bike { void run() { System.out.println("running safely with 100kmph"); } public static void main(String args[]) { Honda honda = new Honda(); honda.run(); } }
- Fix the class declaration:
class Bike { final void run() { System.out.println("running"); } } class Honda extends Bike { void run() { System.out.println("running safely with 100kmph"); } public static void main(String args[]) { Honda honda = new Honda(); honda.run(); } }
- The
final keyword on a method means that it cannot be overridden, meaning this:
For example, I have a class Dog:
public class Dog { final void bark() { System.out.println("Woof!"); } }
And I have a wolf:
class Wolf extends Dog { }
The Wolf class cannot override the bark method, meaning that no matter what, it prints Woof!. However, this might be what you want:
class Bike { void run() { System.out.println("running"); } } class Honda extends Bike { @Override void run() { System.out.println("running safely with 100kmph"); } public static void main(String args[]) { Honda honda = new Honda(); honda.run(); } }