this vs super in Java: Key Differences and Usage

this vs super in Java – Simple Explanation 🎯 🔹 this keyword: ▸ Refers to the current class object ▸ this.variable → access current class variable ▸ this() → call current class constructor ▸ Used to resolve naming conflicts class Student { String name; Student(String name) { this.name = name; // refers to current object } } 🔹 super keyword: ▸ Refers to the parent class object ▸ super.variable → access parent class variable ▸ super() → call parent class constructor ▸ Used to access overridden methods class Dog extends Animal { void sound() { super.sound(); // calls Animal's sound() System.out.println("Bark"); } } 🔑 Key Difference: → this = current class → super = parent class 💡 Both can access constructors, variables, and methods — but direction matters: this → inward | super → upward 🔥 Pro Tip: Use this() and super() carefully they must be the first statement in constructor. #Java #CoreJava #OOP #JavaDeveloper #BackendDeveloper #Programming

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories