Commit 5e462ef
committed
feat: Add TV and SmartTV classes to demonstrate method overriding and polymorphism
WHAT the code does:
Defines TV with methods:
- switchON(): prints "TV is Switched ON".
- chnageChannel(): prints "TV channel is change".
Defines SmartTV extending TV with:
- Overridden switchON(): prints "smart TV is Switched On".
- Overridden chnageChannel(): prints "Smart Tv Browsing".
- New method browse(): prints "Smart Tv Browsing".
Defines Overriding with main():
- Creates a SmartTV object, calls overridden and new methods.
- Demonstrates polymorphism with a TV reference pointing to a SmartTV object.
- Creates a base TV object and calls its methods.
WHY this matters:
Demonstrates **method overriding**:
- Subclass methods replace superclass behavior at runtime.
Illustrates **polymorphism**:
- TV reference pointing to SmartTV executes SmartTV’s methods.
Shows subclass-specific functionality via browse() that is unavailable to superclass references.
Reinforces correct use of the @OverRide annotation.
HOW it works:
SmartTV t1 = new SmartTV():
- Calls switchON(), chnageChannel() → SmartTV versions.
- Calls browse() → only available in SmartTV.
TV t2 = new SmartTV():
- Calls chnageChannel(), switchON() → SmartTV’s overridden versions due to dynamic method dispatch.
TV t = new TV():
- Calls switchON(), chnageChannel() → TV’s original versions.
Tips and gotchas:
Typo in method name: chnageChannel should be changeChannel for clarity and convention.
browse() is only accessible through SmartTV references, not through TV references.
SmartTV st3 = new TV(); is invalid → subclass reference cannot point to superclass object.
@ Override annotation ensures correct method signature overriding; keep consistent capitalization.
Use-cases:
Educational example of method overriding and polymorphism in OOP.
Shows how subclass behavior extends and customizes superclass functionality.
Foundation for real-world hierarchies where base classes define interfaces and subclasses specialize.
Short key: class-tv smarttv overriding polymorphism.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent b127c8f commit 5e462ef
1 file changed
+16
-13
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
4 | 9 | | |
5 | | - | |
6 | | - | |
7 | | - | |
| 10 | + | |
| 11 | + | |
8 | 12 | | |
9 | 13 | | |
10 | 14 | | |
11 | | - | |
12 | | - | |
| 15 | + | |
| 16 | + | |
13 | 17 | | |
14 | 18 | | |
15 | 19 | | |
16 | 20 | | |
17 | 21 | | |
18 | 22 | | |
19 | 23 | | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
24 | 27 | | |
25 | 28 | | |
26 | 29 | | |
| |||
36 | 39 | | |
37 | 40 | | |
38 | 41 | | |
39 | | - | |
| 42 | + | |
0 commit comments