Constructor -
- A constructor is special program which is use to create object often class.
- It is invoke using new keyword.
- It is use to the sign memory to new created object.
- The name of constructor is the name of class but there is no return datatype.
Threre are many type of constructor :
- Default constructor
- Copy constructor
- Argument constructor
Default constructor -
- The simplest kind of constructor that a class can have is the one that has no parameter.
- When you do not explicity define a constructor for a class, then Java creats a default constructor for the class.
- The default constructor automatically initializes all instance variable.
General form - class name class-veriables (or) object-name =new class name ()
Copy consructor -
Examples- Triangle t1 = new triangle ();
Argument Constructor -
निवेदन - आपको यह पोस्ट पसंद आयी हो या आपकी
कोई सुझाव हो तो हमे comment के माध्यम
- A copy constructor is a constructor that replicates (or) duplicates an existing object. So, it is called the copy constructor.
- It takes a parameter is a reference to an object of the same class to which the constructor depend.
Examples- Triangle t1 = new triangle ();
Argument Constructor -
- We cannot add always the above two numbers, so we need values to be added. The easy solution is to add parameter to the constructor.
- The default constructor is often sufficient for simple classes. It is not sophisticated ones. So, we need to declare the argument constructor, the default constructor is no longer used
निवेदन - आपको यह पोस्ट पसंद आयी हो या आपकी
कोई सुझाव हो तो हमे comment के माध्यम
से उल्लेख करे। और अपने दोस्तों के साथ Share करे।
No comments:
Post a Comment