super Keyword in Java: Complete Guide with Examples
The super keyword in Java is a reference variable that points to the
immediate parent class object. It is used to access parent class variables, call
overridden methods, and invoke parent class constructors through super().
In this guide, you will learn every use of super with clear, runnable
examples — including the rules that trip up beginners, such as why super
cannot be used in a static context.
1. Access Parent Class Variable
When both the parent and child classes have a variable with the same name, the child
class variable hides the parent one. The super keyword lets you explicitly
access the parent's variable from a non-static context.
class Parent {
String name = "Parent Name";
}
class Child extends Parent {
String name = "Child Name";
public void printName() {
System.out.println(name);
System.out.println(super.name);
}
public static void main(String[] args) {
Child child = new Child();
child.printName();
}
}
Output:
Child Name
Parent Name
In this program, both classes declare the same variable name name. Inside
the non-static method printName(), super.name gives the parent
class value.
Accessing Both Names from a Static Context
If you want to access both variables from a static context (like main()),
super cannot be used. Instead, use reference variables with type casting.
class Parent {
String name = "Parent Name";
}
class Child extends Parent {
String name = "Child Name";
public static void main(String[] args) {
Child child = new Child();
System.out.println(child.name);
Parent parent = child; // upcasting
System.out.println(parent.name);
}
}
Output:
Child Name
Parent Name
2. Call Overridden Method
When a child class overrides a parent method, super.methodName() lets you
call the parent's version of that method explicitly.
class Parent {
public void song() {
System.out.println("Mukesh Song");
}
}
class Child extends Parent {
public void song() {
System.out.println("Arjit Song");
}
public void listen() {
// Calls Child version
song();
// Calls Parent version via super
super.song();
}
public static void main(String[] args) {
Child child = new Child();
child.listen();
}
}
Output:
Arjit Song
Mukesh Song
Without super, there would be no way to reach the overridden parent method
from inside the child class.
3. Cannot Print super
super is a reference, not a value. Printing it with
System.out.println() is a compile-time error.
class Parent {
}
class Child extends Parent {
public void show() {
System.out.println(super); // compile-time error
}
public static void main(String[] args) {
Child child = new Child();
child.show();
}
}
Output:
Compile-time error
4. Cannot Pass super as Argument
You cannot pass super to a method as an argument. Use
this if you want to pass the current object.
class Parent {
public void show(Parent p) {
System.out.println("Papa");
}
}
class Child extends Parent {
public void call() {
show(super); // compile-time error
}
public static void main(String[] args) {
Child child = new Child();
child.call();
}
}
Output:
Compile-time error
Correct version: use this instead:
public void call() {
show(this); // works fine
}
5. Cannot Return super
You cannot return super from a method. If you need to return the parent
reference, cast the current object or return the parent reference directly.
class Parent {
}
class Child extends Parent {
public Parent show() {
return super; // compile-time error
}
public static void main(String[] args) {
Child child = new Child();
child.show();
}
}
Output:
Compile-time error
Rule: super Cannot Be Used in Static Context
super refers to the parent class object. Since static
methods do not belong to any object, super cannot be used inside them.
supercan only be used in non-static methods and constructors.- Using
superinside a static method causes a compile-time error. - Use a reference variable (like in the first example) if you need similar behavior from a static context.
super() Constructor
super() is used to call a parent class constructor from a child class
constructor. It is the mechanism Java uses for constructor chaining.
1. Calling the Default Parent Constructor
class Parent {
public Parent() {
System.out.println("Hello Default");
}
}
class Child extends Parent {
public Child() {
super();
}
}
public class Demo {
public static void main(String[] args) {
new Child();
}
}
Output:
Hello Default
If the parent class has only a default constructor, Java calls it automatically — you do
not need to write super() explicitly.
class Parent {
public Parent() {
System.out.println("Hello Default");
}
}
class Child extends Parent {
public Child() {
// super() is called implicitly by the compiler
}
}
public class Demo {
public static void main(String[] args) {
new Child();
}
}
Output:
Hello Default
2. Calling a Parameterized Parent Constructor
When the parent class has multiple constructors, the default one is called implicitly
unless you write super(...) explicitly to choose another.
class Parent {
public Parent() {
System.out.println("Hello Default");
}
public Parent(int i) {
System.out.println("Hello parameterized");
}
}
class Child extends Parent {
public Child() {
super(10); // calls Parent(int)
}
}
public class Demo {
public static void main(String[] args) {
new Child();
}
}
Output:
Hello parameterized
Initializing Parent Fields with super()
super(...) can also pass values to the parent constructor, allowing the
parent class to initialize its own fields.
import java.util.Scanner;
class Parent {
String name;
Parent(String name) {
this.name = name;
}
}
class Child extends Parent {
String name;
Child(String name, String pname) {
super(pname); // initializes Parent.name
this.name = name;
}
}
public class Demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter parent name");
String pname = scanner.next();
System.out.println("Enter child name");
String cname = scanner.next();
Child child = new Child(cname, pname);
System.out.println(child.name);
Parent parent = child; // upcasting
System.out.println(parent.name);
scanner.close();
}
}
Output:
Enter parent name
parent
Enter child name
child
child
parent
The parent's name is set through super(pname), and the child's
name is set directly. When the object is referenced as a
Parent, parent.name shows the parent's value.
Important rule: super() must be the first
statement inside a child class constructor. If not, the compiler throws an
error.
Frequently Asked Questions
What is the super keyword in Java?
super is a reference keyword in Java that points to the immediate parent
class object. It is used to access parent class variables, call parent class methods
(especially overridden ones), and invoke parent class constructors via
super().
Can super keyword be used in a static context?
No. super refers to an instance of the parent class, so it can only be used
in non-static methods and constructors. Using super inside a static method
causes a compile-time error.
What is the difference between this and super in Java?
this refers to the current class object; super refers to the
immediate parent class object. this() calls another constructor of the same
class; super() calls a constructor of the parent class. Both must be the
first statement in a constructor and cannot be used together.
Why must super() be the first statement in a constructor?
Java ensures the parent class is fully initialized before the child class constructor
runs. If super() is not the first statement, the compiler throws an error
because the object's inheritance chain must be constructed top-down.
Can we pass super as an argument or return type?
No. super is a special keyword, not a value. It cannot be printed with
System.out.println(), passed as a method argument, or returned from a
method. All of these cause compile-time errors.
Conclusion
The super keyword in Java is essential for working with inheritance. It
gives you access to the parent class from within a child class — whether to read a hidden
variable, call an overridden method, or invoke a parent constructor.
Remember the three rules: super cannot be used in static context, cannot be
printed or passed as a value, and super() must be the first statement in a
constructor. Once these are clear, inheritance in Java becomes much easier to reason
about.
Related topics: this Keyword in Java, Java Inheritance, Method Overriding, Constructor Chaining, Java OOP Concepts, super vs this Comparison
No comments:
Post a Comment