DEV Community

Chhavi Joshi
Chhavi Joshi

Posted on

OOPS in Java(constructor)

in the above code i created 2 classes for which i'll call main function

this gives basic idea of what is a constructor and how it works

TYPES OF CONSTRUCTORS:

  1. Default/ Non parameterized:

this is non parameterized constructor, which would be called whenever an object is created.
In this we'll have to declare its attributes seperately everytime like shown in the main function above.

  1. Parameterized:

this is parameterized constructor where its attributes are mentioned as parameters to be passed whenever creating an object. So its object creation would look as follows:

3.** Copy: **

copy constructor is used to create an object which would be copy of an already existing object. Its syntax is given above and its usage in main class is given as

Top comments (0)