AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-OOPs: What is the purpose of default constructor ?

As If there is no constructor in a class, compiler automatically creates a default constructor. and it doesn´t provide any value. so what´s the use of default constructor.

corejava x 351
OOPs x 49
Posted On : 2013-11-19 23:24:29.0
profile Garima Gupta - anyforum.in Garima Gupta
596129025378
up-rate
3
down-rate

Answers


Constructor is a special type of method that is used to initialize the object. Constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.

Default constructor provides the default values to the object like 0, null etc. depending on the type.

Let´s see the following example :

class Student{  
int id;  
String name;  
 
void display(){System.out.println(id+" "+name);}  
 
public static void main(String args[]){  
Student s1=new Student();  
Student s2=new Student();  
s1.display();  
s2.display();  
}  
}  
It´ll give following output :
0 null  
0 null  

In the above class,you are not creating any constructor so compiler provides you a default constructor. Here 0 and null values are provided by default constructor.

Posted On : 2013-11-19 23:35:53
Satisfied : 2 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523187021394
Reply This Thread
up-rate
4
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in