Skip to content

Commit 2d76078

Browse files
committed
feat: Add ExampleOverriding to demonstrate method overriding and upcasting
WHAT the code does: Defines class car with methods: - start(): prints "Car Started". - accelerate(): prints "Car is Accelerated". - changegear(): prints "Manually We Can Change Car Gear". Defines subclass LuxaryCar extending car: - Overrides start() to print "Baap Hu Tera". - Overrides changegear() to print "Automatic Start and Change Gear". - Adds openRoof(), specific to LuxaryCar. Defines ExampleOverriding with main(): - Creates a LuxaryCar object stored in a car reference (upcasting). - Calls start(), accelerate(), and changegear() to demonstrate polymorphism. WHY this matters: Demonstrates **method overriding**, where subclass methods replace superclass behavior at runtime. Shows **upcasting**: a superclass reference can hold a subclass object. Illustrates polymorphism: overridden methods are resolved by the actual object type (LuxaryCar), not the reference type (car). Reinforces that subclass-specific methods (like openRoof()) cannot be called on a superclass reference. HOW it works: car c = new LuxaryCar(); - c.start() → resolved at runtime → LuxaryCar.start() → prints "Baap Hu Tera". - c.accelerate() → not overridden → car.accelerate(). - c.changegear() → overridden → LuxaryCar.changegear(). - c.openRoof() → invalid because car reference type does not declare it. Tips and gotchas: Class names should follow Java conventions: Car and LuxuryCar instead of car and LuxaryCar. Upcasting improves flexibility but restricts access to subclass-only features unless downcasting is used. System.out.println("Baap Hu Tera") is unconventional—consider clearer domain-appropriate messages for professional code. openRoof() could be accessed if the reference is explicitly cast back to LuxaryCar. Use-cases: Educational demonstration of overriding and polymorphism. Basis for designing extensible systems where subclasses define specific behavior. Foundation for patterns like dependency injection and runtime behavior customization. Short key: class-car luxurycar overriding upcasting polymorphism. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent ce9533f commit 2d76078

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
//Car and LuxaryCar
2-
class car
3-
{
1+
class car {
42
public void start(){System.out.println("Car Started");}
53
public void accelerate(){System.out.println("Car is Accelerated");}
64
public void changegear(){System.out.println("Manually We Can Change Car Gear");}
75
}
8-
class LuxaryCar extends car
9-
{
6+
7+
class LuxaryCar extends car {
108
public void start(){System.out.println("Baap Hu Tera");}
119
public void changegear(){System.out.println("Automatic Start and Change Gear");}
1210
public void openRoof(){System.out.println("Sun Roof is open and Fast Speed and fast moving sports car");}
1311
}
1412

15-
public class ExampleOverriding
16-
{
17-
public static void main(String[] args)
18-
{
13+
public class ExampleOverriding {
14+
public static void main(String[] args) {
1915
car c = new LuxaryCar();
16+
/*you can store a subclass object inside a superclass reference.
17+
This is called upcasting.
18+
19+
“c is a reference of type car, but it is pointing to an object of type LuxaryCar.”
20+
“Superclass reference, subclass object.”*/
2021
c.start();
2122
c.accelerate();
2223
c.changegear();
@@ -29,6 +30,6 @@ public static void main(String[] args)
2930
The reference car c = new LuxaryCar(); demonstrates polymorphism. While c is of type car,
3031
the overridden method in LuxaryCar is executed because the object is of type LuxaryCar.
3132
32-
Downcasting:
33+
Down casting:
3334
To access the openRoof() method, you need to downcast c to LuxaryCar using (LuxaryCar) c.
34-
*/
35+
*/

0 commit comments

Comments
 (0)