Java Super Keyword: Accessing Parent Class Variables and Methods

Hi everyone 👋 Continuing the weekend Java Keyword Series with another important keyword 👇 📌 Java Keyword Series – Part 2 ✅ super keyword in Java The super keyword is used to refer to the immediate parent class object 👇 🔹 Why do we use super? To access parent class variables To call parent class methods To call the parent class constructor 🔹 Where can we use super? 1️⃣ Access parent class variable class Parent { int x = 10; } class Child extends Parent { int x = 20; void show() { System.out.println(super.x); // prints 10 } } 2️⃣ Call parent class method class Parent { void display() { System.out.println("Parent method"); } } class Child extends Parent { void display() { super.display(); // calls parent method System.out.println("Child method"); } } 3️⃣ Call parent class constructor class Parent { Parent() { System.out.println("Parent constructor"); } } class Child extends Parent { Child() { super(); // calls parent constructor } } 🔹 In simple words super is used to access or call members of the parent class. 👉 🧠 Quick Understanding super.variable → parent variable super.method() → parent method super() → parent constructor #Java #CoreJava #JavaKeywords #LearningInPublic #BackendDevelopment

To view or add a comment, sign in

Explore content categories