DEV Community

Chhavi Joshi
Chhavi Joshi

Posted on

OOPS in Java(constructor)

Image description

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

Image description

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

TYPES OF CONSTRUCTORS:

  1. Default/ Non parameterized:

Image description

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:

Image description

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:

Image description

3.** Copy: **

Image description

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

Image description

Top comments (0)