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

Default constructor

What is default constructor? How it access? When it comes into picture? I understood constructor with no arguments is default constructor. Is that ryt.. In same class i defined one explict constructor, and can i call inplicit constructor(default constructor).

16th Aug 2020, 6:10 PM
Haritha Vuppula
Haritha Vuppula - avatar
3 Answers
+ 1
If we dont define a constructor then the compiler creates a default constructor, and if we ourselves write a constructor then the compiler will not create a default constructor. A default constructor provides the default values to the object like 0, null, have a look at this example below public class Student { String name; int age; public static void main(String[] args) { Student s = new Student(); System.out.println(s.name+" "+s.age); } } Output: null 0
16th Aug 2020, 7:19 PM
JavaBobbo
JavaBobbo - avatar
+ 1
If you don't provide any constructor in your class, then compiler provides a default constructor with no arguments and it will set its data values with default values.. Its like, class_name() { //set defaults } If you provide a constructor, then it will be overridden by your constructors hence no longer available.. So you shloud use your own implemented constructor.. Hope it helps.
16th Aug 2020, 6:51 PM
Jayakrishna 🇮🇳
0
~ swim ~ yeah, but it doesnt cleared all my doubts... thats y i came to here iam not clear about default constructor exact in inheritance!
16th Aug 2020, 6:24 PM
Haritha Vuppula
Haritha Vuppula - avatar