How to Use super in Java: Syntax and Examples
In Java,
super is used to refer to the parent class of the current object. You can use super to call a parent class constructor, access parent class methods, or access parent class variables when they are hidden by child class members.Syntax
The super keyword has three main uses in Java:
- Call parent constructor:
super(arguments);must be the first statement in the child constructor. - Call parent method:
super.methodName(arguments);calls a method from the parent class. - Access parent variable:
super.variableNameaccesses a variable from the parent class if hidden by child.
java
class Parent { int x = 10; Parent() { System.out.println("Parent constructor"); } void show() { System.out.println("Parent show method"); } } class Child extends Parent { int x = 20; Child() { super(); // calls Parent constructor System.out.println("Child constructor"); } void show() { super.show(); // calls Parent's show method System.out.println("Child show method"); } void print() { System.out.println(super.x); // accesses Parent's x System.out.println(this.x); // accesses Child's x } }
Example
This example shows how super calls the parent constructor, parent method, and accesses the parent variable when hidden by the child.
java
class Parent { int x = 10; Parent() { System.out.println("Parent constructor called"); } void show() { System.out.println("Parent show method"); } } class Child extends Parent { int x = 20; Child() { super(); // call parent constructor System.out.println("Child constructor called"); } void show() { super.show(); // call parent method System.out.println("Child show method"); } void print() { System.out.println("Parent x: " + super.x); // parent variable System.out.println("Child x: " + this.x); // child variable } } public class Main { public static void main(String[] args) { Child c = new Child(); c.show(); c.print(); } }
Output
Parent constructor called
Child constructor called
Parent show method
Child show method
Parent x: 10
Child x: 20
Common Pitfalls
Common mistakes when using super include:
- Calling
super()not as the first statement in a constructor causes a compile error. - Trying to use
superin static methods is not allowed becausesuperrefers to instance context. - Confusing
super.method()withthis.method()when overriding methods.
java
class Parent { Parent() { System.out.println("Parent constructor"); } } class Child extends Parent { Child() { // Wrong: super() must be first statement System.out.println("Child constructor"); // super(); // compile error - moved to comment } static void staticMethod() { // Wrong: super cannot be used in static context // super.toString(); // compile error } }
Quick Reference
- super() - call parent constructor (must be first in constructor)
- super.method() - call parent class method
- super.variable - access parent class variable
- Cannot use
superin static methods - Use
superto avoid confusion when child overrides parent members
Key Takeaways
Use
super() to call the parent class constructor and it must be the first line in the child constructor.Use
super.method() to call a method from the parent class when overridden in the child.Use
super.variable to access a parent class variable hidden by the child class.Never use
super inside static methods because it refers to instance context.Remember
super helps access parent class members clearly when overridden or hidden.