- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What happens if we call "super()" in a constructor without extending any class, in java?
A super keyword is a reference of the superclass object in Java. Using this, you can invoke the instance methods constructors and, variables, of a superclass.
Calling the constructor of a superclass
You can call the constructor of a superclass from the subclass’s constructor.
Example
Consider the following example, it has two classes SuperClass class and SubClass where the SubClass extends the SuperClass. The superclass has a parameterized constructor, we are invoking it from the subclass using the "super" keyword.
class SuperClass{ SuperClass(int data){ System.out.println("Superclass's constructor: "+data); } } public class SubClass extends SuperClass{ SubClass(int data) { super(data); } public static void main(String args[]){ SubClass sub = new SubClass(400); } }
Output
Superclass's constructor: 400
If the SuperClass has a default Constructor there is no need to call it using"super()" explicitly, it will be invoked implicitly.
Example
class SuperClass{ SuperClass(){ System.out.println("Superclass's constructor"); } } public class SubClass extends SuperClass{ SubClass() { //super(); } public static void main(String args[]){ SubClass sub = new SubClass(); } }
Output
Superclass's constructor
If we call "super()" without any superclass
Actually, nothing will be displayed. Since the class named Object is the superclass of all classes in Java. If you call "super()" without any superclass, Internally, the default constructor of the Object class will be invoked (which displays nothing).
public class Sample { Sample() { super(); } public static void main(String args[]){ Sample sub = new Sample(); } }
Output
//Nothing will be displayed……
- Related Articles
- What happens if we call "super()" in a constructor without extending any class, in java?
- What happens if we try to extend a final class in java?
- What is the super() construct of a constructor in Java?
- Can we call a constructor directly from a method in java?
- Is it possible to create a custom exception in java without extending Exception class?
- Can we call a method on "this" keyword from a constructor in java?
- How can we call one constructor from another in the same class in C#?
- What is the super class of every class in Java?
- Can we define a parameterized constructor in an abstract class in Java?
- How do we check if a class is a subclass of the given super class in Python?
- What happens when you declare a method/constructor final in Java?
- Super constructor in Dart Programming
- How to call the constructor of a parent class in JavaScript?
- How to call parent constructor in child class in PHP?
- How to call the constructor of a superclass from a constructor in java?
- Can you use both this() and super() in a constructor in Java?
