Java OOPC -Java Constructor
Java Constructor
In Java, a constructor is a special type of method that is used to
initialize objects. It is called when an object of a class is created, and its
primary purpose is to set initial values for the object's attributes.
In further we can tell constructor is a method that create using
the class name and it have not any return type while constructor is work on the
time when object is called.
It is a
special type of method which is used to initialize the object.
In simply pointed view we can understand constructor is what
- Method
- Name same of the class name
- No need return type
There are two type of constructors mainly,
1.Parameter less constructor
2.Parameterize constructor
1. Parameter less constructor
In other way we can tell it as default constructor. If a class does
not have any constructor defined, Java provide the parameter less constructor.
In this constructor there is no parameters and perform a basic
initialization.
2. Parameterize constructor
Constructor take parameters, allowing for the initialization of
object with specific values.
Parameterized constructor provide flexibility in object creation.
In there constructor’s parameter list working completely according
to the parameter list and inside the object’s parameter list arguments should
be passed.
In according to the constructor’s parameters, object’s parameters
should be passed.
If we got into know more about parameterize constructor, Below
shows a example for it.
In here, student is the parameterized constructor and
firstname, lastname, age be the asked arguments on there. Ihechikara , abba,100 are the
arguments passed to it.
In below example show the parameter less constructor
student,t here’s no parameter on there and object is created for it and there is
no arguments to passed through.
Comments
Post a Comment