Java Super Keyword (With Examples)
Java Super Keyword (With Examples)
Java Super Keyword (With Examples)
Thank
60% you for printing our content at www.domain-name.com. Please check back soon for new
sale__top-banner-programiz-web__feb__50)
Access
contents.
OFF All
Courses
For Life
Try PRO (https://programiz.pro/learn/master-java?utm_source=right-floating-
for FREE
Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
(https://srv.carbonads.net/ads/click/x/GTND42JUCABIP5QNFT7LYKQNCAS
segment=placement:wwwprogramizcom;) ADS VIA CARBON
(HTTP://CARBONADS.NET/?UTM_SOURCE=WWWPROGRAMIZCOM&UTM_MEDIUM=AD_VIA_LINK&UTM_CAMPAIGN=IN_UNIT&UTM_TERM=CARBON)
Java super
In this tutorial, we will learn about the super keyword in Java with the help of
examples.
Before we learn about the super keyword, make sure to know about Java
inheritance (/java-programming/inheritance).
class Animal {
// overridden method
public void display(){
System.out.println("I am an animal");
}
}
// overriding method
@Override
public void display(){
System.out.println("I am a dog");
}
class Main {
public static void main(String[] args) {
Dog dog1 = new Dog();
dog1.printMessage();
}
}
Output
I am a dog
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
In this
Thank
60% youexample,
for printingby
ourmaking
content anwww.domain-name.com.
at object dog1 of Dog Please
class, check
we can
sale__top-banner-programiz-web__feb__50) call
back itsfor
soon method
new
Access
contents.
OFF
printMessage() All which then executes the display() statement.
Courses
For Life
Since (https://programiz.pro/learn/master-java?utm_source=right-floating-
Try PRO display() is defined in both the classes, the method of subclass Dog
for FREE
Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
overrides the method of superclass Animal . Hence, the display() of the subclass is
www.domain-name.com
called.
needs to be called.
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Example
Thank
60% you for 2: super
printing ourto Call
content atSuperclass Method Please check back soon for new
www.domain-name.com.
sale__top-banner-programiz-web__feb__50)
Access
contents.
OFF All
Courses
For Life
class
Try (https://programiz.pro/learn/master-java?utm_source=right-floating-
PRO Animal {
for FREE
Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
// overridden method
www.domain-name.com
public void display(){
System.out.println("I am an animal");
}
}
// overriding method
@Override
public void display(){
System.out.println("I am a dog");
}
class Main {
public static void main(String[] args) {
Output
I am a dog
I am an animal
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Thank
60% you for printing our content at www.domain-name.com. Please check back soon for new
sale__top-banner-programiz-web__feb__50)
Access
contents.
OFF All
Courses
For Life
Try PRO (https://programiz.pro/learn/master-java?utm_source=right-floating-
for FREE
Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Example
Thank
60% you for 3: Access
printing superclass
our content attribute
at www.domain-name.com. Please check back soon for new
sale__top-banner-programiz-web__feb__50)
Access
contents.
OFF All
Courses
For Life
(https://programiz.pro/learn/master-java?utm_source=right-floating-
class
Try PRO Animal {
for FREE Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
protected String type="animal";
}
www.domain-name.com
class Main {
public static void main(String[] args) {
Dog dog1 = new Dog();
dog1.printType();
}
}
Output:
I am a mammal
I am an animal
In this example, we have defined the same instance field type in both the
superclass Animal and the subclass Dog .
We then created an object dog1 of the Dog class. Then, the printType() method is
called using this object.
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Thank
60% you for printing our content at www.domain-name.com. Please check back soon for new
sale__top-banner-programiz-web__feb__50)
Access
contents.
OFF All
Courses
For Life
(https://programiz.pro/learn/master-java?utm_source=right-floating-
Try PRO
for FREE Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
To explicitly call the superclass constructor from the subclass constructor, we use
super() . It's a special form of the super keyword.
super() can be used only inside the subclass constructor and must be the first
statement.
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Example
Thank
60% you for 4: Useour
printing of content
super() at www.domain-name.com. Please check back soon for new
sale__top-banner-programiz-web__feb__50)
Access
contents.
OFF All
Courses
For Life
(https://programiz.pro/learn/master-java?utm_source=right-floating-
class
Try PRO Animal {
for FREE Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
// default or no-arg constructor of class Animal
www.domain-name.com
Animal() {
System.out.println("I am an animal");
}
}
System.out.println("I am a dog");
}
}
class Main {
public static void main(String[] args) {
Dog dog1 = new Dog();
}
}
Output
I am an animal
I am a dog
Here, when an object dog1 of Dog class is created, it automatically calls the
default or no-arg constructor of that class.
Inside the subclass constructor, the super() statement calls the constructor of the
superclass and executes the statements inside it. Hence, we get the output
I am an animal .
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Thank
60% you for printing our content at www.domain-name.com. Please check back soon for new
sale__top-banner-programiz-web__feb__50)
Access
contents.
OFF All
Courses
For Life
(https://programiz.pro/learn/master-java?utm_source=right-floating-
Try PRO
for FREE Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
The flow of the program then returns back to the subclass constructor and executes
the remaining statements. Thus, I am a dog will be printed.
However, using super() is not compulsory. Even if super() is not used in the
subclass constructor, the compiler implicitly calls the default constructor of the
superclass.
So, why use redundant code if the compiler automatically invokes super()?
The parameterized super() must always be the first statement in the body of the
constructor of the subclass, otherwise, we get a compilation error.
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Example
Thank
60% you for 5: Callour
printing Parameterized
content Constructor Using
at www.domain-name.com. Please super()
check back soon for new
sale__top-banner-programiz-web__feb__50)
Access
contents.
OFF All
Courses
For Life
class
Try (https://programiz.pro/learn/master-java?utm_source=right-floating-
PRO Animal {
for FREE
Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
// default or no-arg constructor
www.domain-name.com
Animal() {
System.out.println("I am an animal");
}
// parameterized constructor
Animal(String type) {
System.out.println("Type: "+type);
}
}
// default constructor
Dog() {
System.out.println("I am a dog");
}
}
class Main {
public static void main(String[] args) {
Dog dog1 = new Dog();
Output
Type: Animal
I am a dog
The compiler can automatically call the no-arg constructor. However, it cannot call
parameterized constructors.
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Thank
60% you for printing our content at www.domain-name.com. Please check back soon for new
sale__top-banner-programiz-web__feb__50)
Access
contents.
OFF All
Courses
For Life
(https://programiz.pro/learn/master-java?utm_source=right-floating-
Try PRO
for FREE Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
Note that in the above example, we explicitly called the parameterized constructor
super("Animal") . The compiler does not call the default constructor of the
superclass in this case.
Next Tutorial:
(/java-programming/abstract-classes-methods)
Abstract Class & Method
Previous Tutorial:
(/java-programming/method-overriding)
Java Method Overriding
Share on:
(https://www.facebook.com/sharer/sharer.php? (https://twitter.com/intent/tweet?
u=https://www.programiz.com/java- text=Check%20this%20amazing%20
programming/super-keyword) programming/super-keyword)
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Thank
60% you for printing our content at www.domain-name.com. Please check back soon for new
sale__top-banner-programiz-web__feb__50)
Access
contents.
OFF All
Courses
For Life
(https://programiz.pro/learn/master-java?utm_source=right-floating-
Try PRO
for FREE Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
Related Tutorials
Java Tutorial
(/java-programming/method-overriding)
Java Tutorial
Java Inheritance
(/java-programming/inheritance)
Java Tutorial
(/java-programming/abstract-classes-methods)
Java Tutorial
OfferOperator
Java instanceof
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Thank
60% you for printing our content at www.domain-name.com. Please check back soon for new
sale__top-banner-programiz-web__feb__50)
Access
contents.
(/java-programming/instanceof)
OFF All
Courses
For Life
(https://programiz.pro/learn/master-java?utm_source=right-floating-
Try PRO
for FREE Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com