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

Java की super Keyword

super वह तरीका है जिससे एक child class अपने parent को refer करता है, 'मेरी mom से पूछो' कहने जैसा। आप इसे parent के methods और setup call करने के लिए उपयोग कर सकते हैं।
Syntax
java
super.methodName();
super(arguments);  // call parent constructor
super.field;

What is the super Keyword?

super एक subclass के अंदर उपयोग होने वाला एक reference है इसके immediate parent class तक explicitly पहुंचने के लिए — इसके members, इसके constructors — उन्हीं names के subclass के अपने overridden या shadowed versions के बजाय।

उदाहरण: What is the super Keyword?

java
class Animal {
	void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
	@Override
	void sound() {
		super.sound(); // explicitly reaches the parent's version
		System.out.println("Bark");
	}
}
public class Main {
	public static void main(String[] args) {
		new Dog().sound();
	}
}

Accessing Parent Variables

जब एक subclass field या method parent में किसी चीज़ के साथ नाम share करता है और effectively इसे hide करता है, super.fieldName या super.methodName() आपको जानबूझकर subclass के version से आगे parent के original तक पहुंचने देता है।

उदाहरण: Accessing Parent Variables

java
// Define the class Animal
class Animal {
	// Declare type and set it to "Animal"
	String type = "Animal";
}
// Define the class Dog
class Dog extends Animal {
	// Declare type and set it to "Dog"
	String type = "Dog";
	void show() {
		// Print a line to the console
		System.out.println(type);
		// Print a line to the console
		System.out.println(super.type);
	}
}
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		new Dog().show();
	}
}

Calling Parent Constructors

super(...) explicitly एक subclass constructor के अंदर से एक parent class constructor invoke करता है, और Java चाहता है यह call (अगर मौजूद हो) subclass constructor body में बिल्कुल पहला statement हो, किसी भी दूसरी initialization होने से पहले।

उदाहरण: Calling Parent Constructors

java
class Animal {
	Animal() {
		System.out.println("Animal created");
	}
}
class Dog extends Animal {
	Dog() {
		super(); // must be the first statement
		System.out.println("Dog created");
	}
}
public class Main {
	public static void main(String[] args) {
		new Dog();
	}
}

Parameterized Parent Constructors

अगर parent class में कोई no-argument constructor नहीं — सिर्फ एक parameterized एक — subclass constructor को matching arguments के साथ explicitly super(arguments) call करना चाहिए, क्योंकि Java चुपचाप एक ऐसे constructor की call insert नहीं करेगी जो मौजूद नहीं।

उदाहरण: Parameterized Parent Constructors

java
class Animal {
	Animal(String name) {
		System.out.println("Animal: " + name);
	}
}
class Dog extends Animal {
	Dog() {
		super("Rex"); // required: Animal has no no-arg constructor
	}
}
public class Main {
	public static void main(String[] args) {
		new Dog();
	}
}

Super with Nested Hierarchies

एक multilevel hierarchy में, super हमेशा बिल्कुल एक level ऊपर resolve होता है — immediate parent — कभी आगे grandparent तक skip नहीं करता, जो chain के through constructor और method delegation को step by step predictable रखता है।

उदाहरण: Super with Nested Hierarchies

java
class A {
	void show() { System.out.println("A"); }
}
class B extends A {
	@Override
	void show() { System.out.println("B"); }
}
class C extends B {
	void callSuper() {
		super.show(); // resolves to B, not A
	}
}
public class Main {
	public static void main(String[] args) {
		new C().callSuper();
	}
}
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. एक constructor में दूसरे statements के बाद super() call करना, जो एक compile error है।
  2. एक static method में super उपयोग करना, जो allowed नहीं।
  3. यह उम्मीद करना कि super.method() grandparent को call करेगा, जब यह direct parent का version call करता है।

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.