Java की super Keyword
In this page:
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?
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();
}
}
Login to try C/C++/Java/PHP code in the editor
Accessing Parent Variables
जब एक subclass field या method parent में किसी चीज़ के साथ नाम share करता है और effectively इसे hide करता है, super.fieldName या super.methodName() आपको जानबूझकर subclass के version से आगे parent के original तक पहुंचने देता है।
उदाहरण: Accessing Parent Variables
// 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();
}
}
Login to try C/C++/Java/PHP code in the editor
Calling Parent Constructors
super(...) explicitly एक subclass constructor के अंदर से एक parent class constructor invoke करता है, और Java चाहता है यह call (अगर मौजूद हो) subclass constructor body में बिल्कुल पहला statement हो, किसी भी दूसरी initialization होने से पहले।
उदाहरण: Calling Parent Constructors
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();
}
}
Login to try C/C++/Java/PHP code in the editor
Parameterized Parent Constructors
अगर parent class में कोई no-argument constructor नहीं — सिर्फ एक parameterized एक — subclass constructor को matching arguments के साथ explicitly super(arguments) call करना चाहिए, क्योंकि Java चुपचाप एक ऐसे constructor की call insert नहीं करेगी जो मौजूद नहीं।
उदाहरण: Parameterized Parent Constructors
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();
}
}
Login to try C/C++/Java/PHP code in the editor
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
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();
}
}
Login to try C/C++/Java/PHP code in the editor
- एक constructor में दूसरे statements के बाद
super()call करना, जो एक compile error है। - एक static method में
superउपयोग करना, जो allowed नहीं। - यह उम्मीद करना कि
super.method()grandparent को call करेगा, जब यह direct parent का version call करता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: