OOP Advanced: sealed, static, partial

This is where interviews separate beginners from devs. Master this = job offers.

60-Second Version: sealed = no more inheritance. static class = utility only, no objects. new keyword = hide method, breaks polymorphism. Use override instead.

1. sealed: Stop Inheritance Chain

Used on String class. No one can inherit from String.

public sealed class SecurityToken { } // No child allowed
// public class MyToken : SecurityToken {} // Compiler Error

sealed override = child can't override this method further.

2. static Class: Toolbox, Not Blueprint

public static class MathHelper // No objects
{
    public static int Square(int x) => x * x;
    public static double PI = 3.14;
}
// MathHelper m = new MathHelper(); // Error: Cannot create instance
int result = MathHelper.Square(5); // Use directly

3. Method Hiding new vs Override - The #1 Interview Question

public class Car { 
    public virtual void Start() { Console.WriteLine("Car Start"); }
}

public class SportsCar : Car { 
    public new void Start() { Console.WriteLine("VROOM"); } // Hides
}

Car c = new SportsCar();
c.Start(); // Output: Car Start - Uses variable type, NOT object type
// This is why we use override for true polymorphism
Real-World Trap: new breaks polymorphism. If you see new in interview code, it's usually a bug or trick question. Always use virtual + override.

Quick Check ๐Ÿง 

Next: OOP Final Recap & Mega Quiz - 15 questions. If you pass this, you're interview ready.

Comments on OOP Advanced (0)

No comments yet. Be the first to share your thoughts!