Important question | Sololearn: Learn to code for FREE!

0

Important question

It the class must always have a default constructor??

4/23/2021 10:43:06 PM

Lisa John

14 Answers

New Answer

+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.

+4

You can try it yourself in code Playground

+2

Benjamin Jürgens So it is false?

+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

+2

Thanks all of you

+2

Default constructor will be added by the compiler if you create an object in that class

+2

there's no need for that ... If u don't include that compiler automatically creates default constructor

+1

Benjamin Jürgens :) i want help just tell me it is true or false?

+1

public class Program { public static void main(String[] args) { } } No constructor

+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.

+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

+1

Depends upon the usage and application. If you want to some default values to get initialized. If you are writing normal program you won't get much insights of it. But, if you go in real world, say any application. This default constructor peforms a great role. Let's take example of this SoloLearn Application. As soon you tap on the application icon os SoloLearn to launch. It takes some times before you land on the homepage. Here comes the usage of default constructor that gets the necessary permission from android, fetches your saved profiles and progress from phone storage and other required files to start the application properly. So, the things which are shown to be by default are performed with the help of default constructors. Hope, you got a little idea of default constructor. This was just a single example for understanding purpose with real life application.

0

You don't have to provide any construction for your class , but you must be careful when doing this.

-3

Good for 33 330 l. 0%l0....