The super keyword in Java refers to the immediate parent class of the current object. You use it to call a parent constructor with super(...), to call a parent method that the subclass has overridden with super.method(), and to reach a parent field that the subclass hides with super.field.
It only makes sense inside instance code (constructors, instance methods, instance initialisers), because it is bound to the current object rather than to a class name. This article shows each use with runnable code, the rules the compiler enforces, and the related form Interface.super.method() for default methods.
super(...)
super.method()super.fieldInterface.super.method()super is not allowedsuper compared with thissuper(...)Every constructor of a subclass must run a constructor of its superclass first, so the parent part of the object is initialised before the child part. If you do not write the call yourself, the compiler inserts a call to the no-argument parent constructor super(). When the parent has no accessible no-argument constructor, you must call one of its constructors explicitly.
Because Vehicle declares only a constructor that takes a String, removing the super(name) line produces a compile error: the implicit super() has nothing to call.
super(...) and this(...)super(...) or this(...) as its explicit constructor call, not both.this or call instance methods. On earlier releases the call must still come first.super(...), and a record's canonical constructor cannot either.super.method()When a subclass overrides a method, super.method() runs the parent version. This is the standard way to extend behaviour instead of replacing it.
Without super., the call log(...) inside log would call itself and recurse until a StackOverflowError. super.method() is resolved without dynamic dispatch, so it always calls the parent's implementation, even if a further subclass overrides it again.
You can only reach the immediate parent this way. super.super.method() is not valid Java. If you need a grandparent implementation, expose it through a helper method in the parent.
super.fieldA field in a subclass with the same name as a parent field hides it rather than overriding it. super.field reads the parent's copy.
Hiding fields is almost always a design smell, because the two fields hold independent values and the one you get depends on the static type of the reference. Prefer different names, or keep fields private and expose them through methods.
Interface.super.method()Since Java 8 an interface can supply default methods. If a class inherits the same default method from two interfaces, it must override the method itself, and it can pick one implementation with the qualified form InterfaceName.super.method().
new Duck().move() returns walking and swimming. The interface must be a direct superinterface of the class; you cannot skip a level.
super is not allowedstatic methods and static initialisers. There is no current object, so super and this are both unavailable.private method or field. Private members are not inherited, so super.secret() does not compile.super is not a value, so Object o = super; is invalid.Do not confuse super with generics wildcards. In List<? super Integer> the word means a lower bound on a type, which is an unrelated feature of the type system.
super compared with this| Aspect | this | super |
|---|---|---|
| Refers to | The current object | The parent part of the current object |
| Constructor call | this(...) calls another constructor of the same class | super(...) calls a parent constructor |
| Method call | this.m() uses normal dynamic dispatch | super.m() calls the parent version directly |
| Field access | The field visible in the current class | The parent field hidden by the current class |
super() added automatically?Yes. If a constructor does not start with this(...) or super(...), the compiler adds super(). This fails to compile when the parent has no accessible no-argument constructor.
super.method() from a static method?No. Static methods have no instance, so the compiler rejects super there. Call the parent's static method by class name, for example Parent.helper().
super work with the Object class?Every class without an explicit parent extends Object, so super.toString(), super.equals(...) and super.hashCode() call the Object implementations. This is common inside overridden toString() methods.
super.method() and overriding?Overriding replaces the parent's behaviour for the subclass. super.method() is how an overriding method still reuses the parent behaviour as part of its own.
Use super(...) to run a parent constructor, super.method() to reuse an overridden parent method, and super.field to read a hidden parent field. Interface.super.method() resolves conflicting default methods. super always refers to the immediate parent, only works in instance code, and the compiler inserts super() for you when you leave it out.