← Back to Core Java Course | Chapter 9: OOP Advanced | Lesson 1 of 8

Java में Polymorphism

Polymorphism का मतलब है एक command object के आधार पर अलग तरीकों से काम कर सकता है, speak एक dog को bark कराता और एक cat को meow कराता है जैसा। आप अलग चीज़ों को एक जैसा treat कर सकते हैं।
Syntax
java
ParentType variable = new ChildType();
variable.methodName();  // child's version runs

What is Polymorphism?

Polymorphism ('कई forms') अलग classes के objects को एक shared, common interface या parent type के through treat होने देता है, इसलिए calling code की वही line सही से अलग behavior trigger कर सकती है इस आधार पर कि यह किस असली object पर operate कर रही है।

उदाहरण: What is Polymorphism?

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(); // same call, different behavior depending on the object
	}
}

Compile-Time Polymorphism

Method overloading के ज़रिए हासिल किया गया Compile-time polymorphism, एक class को कई methods define करने देता है जो एक नाम share करते हैं लेकिन उनकी parameter lists में अलग होते हैं — compiler compile time पर arguments के types के आधार पर call करने के लिए सही version चुनता है।

उदाहरण: Compile-Time Polymorphism

java
// Define the class Main
public class Main {
	static int add(int a, int b) { return a + b; }
	static double add(double a, double b) { return a + b; }
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(add(2, 3));
		// Print a line to the console
		System.out.println(add(2.5, 3.5));
	}
}

Run-Time Polymorphism

Method overriding के ज़रिए Run-time polymorphism हासिल होता है, और कौन सा overridden method असल में चलता है इसका decision JVM द्वारा runtime पर object के असली, concrete class के आधार पर लिया जाता है — compiler द्वारा reference variable के declared type के आधार पर नहीं।

उदाहरण: Run-Time Polymorphism

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(); // decided at runtime by the object's real class
	}
}

Polymorphism with Data Members

Java में सिर्फ methods dynamic runtime dispatch में participate करते हैं; fields नहीं — अगर एक subclass उसी नाम वाली एक field से parent की field hide करता है, आपको कौन सी field वापस मिलती है यह compile time पर reference type पर निर्भर करता है, object की असली runtime class पर नहीं।

उदाहरण: Polymorphism with Data Members

java
class Animal {
	String type = "Animal";
}
class Dog extends Animal {
	String type = "Dog";
}
public class Main {
	public static void main(String[] args) {
		Animal a = new Dog();
		System.out.println(a.type); // "Animal": fields don't use dynamic dispatch
	}
}

Benefits of Polymorphism

Polymorphism loose coupling को promote करता है आपको ऐसे functions और methods लिखने देकर जो एक general parent type accept करते हैं और pass किए किसी भी subclass के साथ सही से काम करते हैं, पहले से हर possible subclass जानने की ज़रूरत के बिना जो आखिरकार मौजूद हो सकता है।

उदाहरण: Benefits of Polymorphism

java
class Animal {
	void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
	@Override
	void sound() { System.out.println("Bark"); }
}
class Cat extends Animal {
	@Override
	void sound() { System.out.println("Meow"); }
}
public class Main {
	static void makeSound(Animal a) { // works with any subclass
		a.sound();
	}
	public static void main(String[] args) {
		makeSound(new Dog());
		makeSound(new Cat());
	}
}
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. एक parent reference के through सिर्फ child में मौजूद एक method call करना, जो एक compile error है।
  2. Fields के polymorphic होने की उम्मीद करना, जब वे reference type से चुने जाते हैं।
  3. Overloaded methods के run time पर चुने जाने की उम्मीद करना, जब overloads compile time पर चुने जाते हैं।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

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.