← Back to Core Java Course | Chapter 8: Inheritance | Lesson 5 of 7

Java में Method Overriding

Method overriding तब है जब एक child class parent पहले से करता है कुछ का अपना version देती है, एक chore अपने तरीके से करने वाले एक child जैसा। Java child का version चलाता है।
Syntax
java
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?

java
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
	}
}

Overriding Rules

एक override valid होने के लिए, subclass method को नाम और parameter list में parent के method से बिल्कुल match करना चाहिए, और इसका return type वही या एक covariant (ज़्यादा specific) subtype होना चाहिए — इनमें से किसी को mismatch करना एक असली override के बजाय एक नया overloaded method बनाता है।

उदाहरण: Overriding Rules

java
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();
	}
}

The @Override Annotation

@Override annotation strictly required नहीं है, लेकिन यह compiler को बताता है कि आपका इरादा एक parent method override करने का है, इसलिए अगर आप गलती से signature mistype करें, compiler इसे चुपचाप आपको एक unrelated नया method बनाने देने के बजाय एक error के रूप में flag करता है।

उदाहरण: The @Override Annotation

java
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();
	}
}

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

java
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
	}
}

Preventing Overriding with final

एक parent method को final मार्क करना किसी भी subclass को इसे बिल्कुल override करने से रोकता है, जो सही tool है जब एक method के behavior को हर जगह जहां यह inherited है identically guaranteed होना चाहिए, जैसे core validation logic जिसे आप कभी किसी subclass को bypass नहीं कराना चाहते।

उदाहरण: Preventing Overriding with final

java
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();
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Method signature बदलना, जो एक overload बनाता है override नहीं।
  2. @Override भूल जाना, इसलिए एक typo चुपचाप एक नया method बनाता है।
  3. Visibility घटाना, जैसे एक public method को protected के रूप में override करना, जो एक compile error है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.