Skip to content

Commit 220fc5e

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 95e1350 commit 220fc5e

File tree

1 file changed

+8
-13
lines changed
  • Section16StaticFinal/src/vehicles

1 file changed

+8
-13
lines changed
Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
11
package vehicles;
22

3-
public class car extends Vehicle
4-
{
3+
public class car extends Vehicle {
54
// Constant for Pi, defined using final static for immutability
65
public static final double PI = 3.14159;
76

87
public int speedLimit;
98

109
// Constructor for Car class
11-
public car()
12-
{
13-
speedLimit = 200; // Initializing speedLimit
10+
public car() {
11+
speedLimit = 200; // Initializing speedLimit.
1412
}
1513

16-
// Implementing accelerate method
14+
// Implementing accelerate method.
1715
@Override
18-
public void accelerate()
19-
{
16+
public void accelerate() {
2017
System.out.println("Car is accelerating...");
2118
}
2219

2320
// Implementing decelerate method
2421
@Override
25-
public void decelerate()
26-
{
22+
public void decelerate() {
2723
System.out.println("Car is decelerating...");
2824
}
2925

3026
// This method is final, meaning it cannot be overridden in subclasses
31-
public final void airBags()
32-
{
27+
public final void airBags() {
3328
System.out.println("4 Air Bags");
3429
}
35-
}
30+
}

0 commit comments

Comments
 (0)