Skip to content

Commit 5e462ef

Browse files
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

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1-
//Need Practice the same thing again.
2-
//Solve Again Revise This.
3-
//https://abdulbari.newzenler.com/courses/java-se/contents/5fcf6ad0df944
1+
/**
2+
* 📘 Java SE Course – Abdul Bari
3+
* <p>
4+
* 📝 Need Practice – Solve Again & Revise This
5+
* <p>
6+
* Course Content: <a href="https://abdulbari.newzenler.com/courses/java-se/contents/5fcf6ad0df944">
7+
* https://abdulbari.newzenler.com/courses/java-se/contents/5fcf6ad0df944</a>
8+
*/
49

5-
class TV
6-
{
7-
//Added annotaion.
10+
class TV {
11+
//Added annotation.
812
public void switchON(){System.out.println("TV is Switched ON");}
913
public void chnageChannel(){System.out.println("TV channel is change");}
1014
}
11-
class SmartTV extends TV
12-
{
15+
16+
class SmartTV extends TV {
1317
@Override
1418
public void switchON(){System.out.println("smart TV is Switched On");}
1519
@Override
1620
public void chnageChannel(){System.out.println("Smart Tv Browsing");}
1721

1822
public void browse(){System.out.println("Smart Tv Browsing");}
1923
}
20-
public class Overriding
21-
{
22-
public static void main(String[] args)
23-
{
24+
25+
public class Overriding {
26+
public static void main(String[] args) {
2427
SmartTV t1=new SmartTV();
2528
t1.switchON();
2629
t1.chnageChannel();
@@ -36,4 +39,4 @@ public static void main(String[] args)
3639
t.switchON();
3740
t.chnageChannel();
3841
}
39-
}
42+
}

0 commit comments

Comments
 (0)