Skip to content

Commit 7bb2c6b

Browse files
committed
feat: Implement Vehicle hierarchy with Car, EVCar, abstract methods, and final keyword usage
WHAT the code does: - Declares abstract class Vehicle: - Defines two abstract methods: accelerate() and decelerate(). - Defines class car extending Vehicle: - Has a constant PI (static final, immutable). - Field speedLimit initialized to 200 in the constructor. - Implements accelerate() and decelerate() methods. - Declares final method airBags(), which prints "4 Air Bags" and cannot be overridden by subclasses. - Defines class EVCar extending car: - Provides its own implementations of accelerate() and decelerate(). - Shows (commented-out) that overriding airBags() would cause a compile-time error because it is final. - Defines class Test with main(): - Creates a car instance and demonstrates speedLimit, accelerate(), decelerate(), and airBags(). - Creates an EVCar instance and demonstrates overridden accelerate() and decelerate() methods. WHY this matters: - Demonstrates **abstraction**: Vehicle defines contracts, subclasses implement them. - Demonstrates **inheritance and overriding**: Car and EVCar provide their own implementations. - Demonstrates **final keyword usage**: - final variable (PI) → constant, cannot be reassigned. - final method (airBags) → prevents overriding in subclasses. - (Comment in code) final class → cannot be subclassed. - Constructors cannot be final (they’re never inherited/overridden). - Illustrates polymorphism and restrictions that enforce immutability and design constraints. HOW it works: 1. Vehicle is abstract and cannot be instantiated; only concrete subclasses can. 2. Car implements Vehicle’s methods and adds its own final method airBags(). 3. EVCar extends Car, overrides accelerate() and decelerate(), but cannot override airBags(). 4. Test.main(): - myCar calls accelerate(), decelerate(), and airBags(). - myEVCar uses EVCar’s overridden accelerate() and decelerate(), and inherits airBags(). Tips and gotchas: - Class names should follow Java naming conventions (Car, not car). - Constants like PI are better placed in a utility class or java.lang.Math, but the example demonstrates final variables well. - Overuse of final on methods can limit extensibility — use when you intentionally want to lock behavior. - Abstract classes can combine abstract and concrete methods, unlike interfaces prior to Java 8. - EVCar inherits airBags() but cannot redefine it; if design requires variation, avoid final. Use-cases / analogies: - Car vs EVCar: both accelerate and decelerate but behave differently. - Final method airBags: similar to mandatory government regulation — all cars must have at least 4 airbags, subclasses cannot change that rule. - Final variables: physical constants (like PI) that never change. - Abstract Vehicle: a general blueprint for all types of vehicles. Short key: java-oop abstraction inheritance overriding final-variable final-method vehicle-hierarchy. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent dc8abf1 commit 7bb2c6b

File tree

1 file changed

+0
-3
lines changed

1 file changed

+0
-3
lines changed
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package vehicles;
22

33
public abstract class Vehicle {
4-
54
public abstract void accelerate();
6-
75
public abstract void decelerate();
8-
96
}

0 commit comments

Comments
 (0)