Important question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Important question

It the class must always have a default constructor??

23rd Apr 2021, 10:43 PM
Lisa John
Lisa John - avatar
13 Answers
+ 3
Every class written in java by default has its parent class 'Object'. So when ever you create a new instance of your class it will directly calls it's parents class constructor .. so the answer for your question is not required. But if you want your own implementation for your constructor.. you need to create one which will override your parent class constructor.
25th Apr 2021, 10:30 AM
Pamidisetti Manikanta
Pamidisetti Manikanta - avatar
+ 4
You can try it yourself in code Playground
23rd Apr 2021, 11:27 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 2
Benjamin Jürgens So it is false?
23rd Apr 2021, 11:40 PM
Lisa John
Lisa John - avatar
+ 2
"You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor." https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
24th Apr 2021, 7:08 AM
Ciro Pellegrino
Ciro Pellegrino - avatar
+ 2
Thanks all of you
24th Apr 2021, 10:13 AM
Lisa John
Lisa John - avatar
+ 2
Default constructor will be added by the compiler if you create an object in that class
24th Apr 2021, 10:57 AM
KIRAN S
KIRAN S - avatar
+ 2
there's no need for that ... If u don't include that compiler automatically creates default constructor
24th Apr 2021, 1:32 PM
kreddyt
kreddyt - avatar
+ 1
Benjamin Jürgens :) i want help just tell me it is true or false?
23rd Apr 2021, 11:29 PM
Lisa John
Lisa John - avatar
+ 1
public class Program { public static void main(String[] args) { } } No constructor
23rd Apr 2021, 11:39 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 1
Yes, a program has its own default constructor which is called automatically at the time of object creation. For example:- Like we have a program of class name Counter Then,as we create a object(object name- count)of class Counter a default constructor will be called by itself. e.g.- Counter count=new Counter(); You can see that in above object creation statement, at the last Counter() constructor is called.
24th Apr 2021, 4:01 AM
Aman Jain
Aman Jain - avatar
+ 1
Not necessary when you are not calling for a custom class. And complier also creates a default constructor from its own when it doesn't get the default constructor
24th Apr 2021, 4:55 AM
Atul [Inactive]
0
You don't have to provide any construction for your class , but you must be careful when doing this.
24th Apr 2021, 9:58 AM
...Mansi...
...Mansi... - avatar
- 3
Good for 33 330 l. 0%l0....
25th Apr 2021, 4:54 PM
LinLyn Mingo Andaliston
LinLyn Mingo Andaliston - avatar