📘 Module 4: Functions & Methods in Java

📊 Java Method Overriding – Flowchart (Text Form)

┌────────────────────────┐
│     Parent Class       │
│------------------------│
│ methodName()           │
│ → Default implementation│
└──────────┬─────────────┘
           │
           ▼
┌────────────────────────────┐
│   Child Class extends Parent│
│-----------------------------│
│ @Override                   │
│ methodName()               │
│ → New implementation       │
└──────────┬─────────────────┘
           │
           ▼
┌──────────────────────────────┐
│ Parent reference = new Child(); │
│ reference.methodName();        │
└──────────┬────────────────────┘
           ▼
     Runtime Decision:
     Executes Child’s method

📘 Quick Reference Table: Overriding vs Overloading

FeatureMethod OverridingMethod Overloading
ClassParent-child classes (Inheritance)Same class
ParametersSameDifferent
Return TypeSame (or covariant)Can be different
Polymorphism TypeRuntimeCompile-time
AnnotationUses @Override (recommended)कोई annotation नहीं ज़रूरी
Access ModifierCannot be more restrictiveकोई restriction नहीं
Static Methods❌ Static methods can’t be overridden✅ Can be overloaded

✅ Important Rules for Overriding:

  • Method name, parameters और return type same होने चाहिए
  • Access modifier equal या ज़्यादा accessible होना चाहिए (public > protected > default > private)
  • Overriding only works with instance methods (non-static)
  • Private methods, static methods, और constructors override नहीं हो सकते