Java में Method Overriding
In this page:
class Parent {
returnType methodName() { }
}
class Child extends Parent {
@Override
returnType methodName() { }
}
What is Method Overriding?
Method overriding एक subclass को parent class पहले से define करती एक method का अपना implementation देने देता है, जो runtime polymorphism के नीचे का mechanism है — एक parent-typed reference के through method call करने वाला code अब भी subclass का specific behavior पाता है।
उदाहरण: What is Method Overriding?
class Animal {
void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
@Override
void sound() { System.out.println("Bark"); }
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.sound(); // Bark, decided by actual object type
}
}
Login to try C/C++/Java/PHP code in the editor
Overriding Rules
एक override valid होने के लिए, subclass method को नाम और parameter list में parent के method से बिल्कुल match करना चाहिए, और इसका return type वही या एक covariant (ज़्यादा specific) subtype होना चाहिए — इनमें से किसी को mismatch करना एक असली override के बजाय एक नया overloaded method बनाता है।
उदाहरण: Overriding Rules
class Animal {
void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
@Override
void sound() { // same name, same parameters, same return type
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
new Dog().sound();
}
}
Login to try C/C++/Java/PHP code in the editor
The @Override Annotation
@Override annotation strictly required नहीं है, लेकिन यह compiler को बताता है कि आपका इरादा एक parent method override करने का है, इसलिए अगर आप गलती से signature mistype करें, compiler इसे चुपचाप आपको एक unrelated नया method बनाने देने के बजाय एक error के रूप में flag करता है।
उदाहरण: The @Override Annotation
class Animal {
void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
@Override // compiler verifies this actually overrides a parent method
void sound() { System.out.println("Bark"); }
}
public class Main {
public static void main(String[] args) {
new Dog().sound();
}
}
Login to try C/C++/Java/PHP code in the editor
Dynamic Method Dispatch
Dynamic method dispatch वह है जो runtime पर decide करता है कि एक overridden method का कौन सा version असल में execute होता है — JVM object के असली, concrete type को देखता है, उस variable के type को नहीं जिससे यह referenced है, यही कारण है कि polymorphism parent-typed references के through सही से काम करता है।
उदाहरण: Dynamic Method Dispatch
class Animal {
void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
@Override
void sound() { System.out.println("Bark"); }
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.sound(); // JVM picks Dog's version at runtime, not Animal's
}
}
Login to try C/C++/Java/PHP code in the editor
Preventing Overriding with final
एक parent method को final मार्क करना किसी भी subclass को इसे बिल्कुल override करने से रोकता है, जो सही tool है जब एक method के behavior को हर जगह जहां यह inherited है identically guaranteed होना चाहिए, जैसे core validation logic जिसे आप कभी किसी subclass को bypass नहीं कराना चाहते।
उदाहरण: Preventing Overriding with final
class Animal {
final void identify() {
System.out.println("I am an animal");
}
}
class Dog extends Animal {
// void identify() {} // would not compile: identify() is final
}
public class Main {
public static void main(String[] args) {
new Dog().identify();
}
}
Login to try C/C++/Java/PHP code in the editor
- Method signature बदलना, जो एक overload बनाता है override नहीं।
@Overrideभूल जाना, इसलिए एक typo चुपचाप एक नया method बनाता है।- Visibility घटाना, जैसे एक
publicmethod कोprotectedके रूप में override करना, जो एक compile error है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: