Class As User Defined Data Type and Constructor
Class As User Defined Data Type and Constructor
5. What is Constructor?
Constructor is a member method that is written with the same name as class name and is used to
initialize the data members/instance variable. It is invoked at the time of creating any object of
the class.
CONSTRUCTOR METHOD
a)It has same name as class name. a)It has same or different name as class
name.
b)It is implicitly called. b)It is explicitly called.
c)It does not return any value. c)It may or may not return a value.
d)It is always public. d)It may public, private or protected.
Public – The data members or member methods which are specified as public can be used within
the scope of a class or can be used even outside of the class.
Protected – The data members or member methods which are specified as protected can be used
only within the scope of a class as private members . These members can be used to another
class during inheritance.
Advantages of array :-
a) Initialization can be done in a single line statement,
b) Java arrays are dynamic as memory allocation is done at run time.
c) It is possible access any element provided the index is known.
d) A huge number of data can be represented by a single variable name but different index
value.
Disadvantages of array :-
a) Arrays allows us to store only one data type.
b) It always require contiguous free memory location to be allocated for sorting elements.