+ 1
Do default constructor called automatically while creating object even If I don't provide any statement in it?
Please do explain.
5 Answers
+ 7
Constructors are the code that is used to create objects.
Constructors start with the short name of the class, followed by parentheses, that contain any parameters, and then an opening curly brace and a closing curly brace. In between the curly braces you assign values to instance variables and call any methods that need to be called when an object of this class type is created.
Constructors may look like methods, but they neither return a value nor do they have the tag 'void'. You can have as many constructors as you want in a class, but each constructor needs a unique set of parameters.
If you donât write a constructor for your class, the compiler will implicitly include a constructor that contains no parameters.
+ 2
Before I start next time when posting please tag the language youre talking about, it makes it much easier and faster to help you :) From your profile I will assume youâre talking about Java. Default constructors are made by the compiler if no custom ones are provided in a class and are called just like user defined ones, default constructors are completely empty something like this:
class MyClass {
public MyClass() {}
}
So when you create an object you cannot pass any arguments
+ 1
Jamilur Rahman should be mostly the same
0
TurtleShell Thanks for correcting me. But, What about other languages? To be specific, I want to know in the way of Object Oriented Programming. That's why didn't include specefic language.
0
TurtleShell Okay, Thanks.
Waiting for more persons to comment. đ