Sunday, June 22, 2014

Java Constructor

Constructor is a special member function of a class that is used to initialize the object. If a class has a Constructor, Java automatically calls that Constructor when an object is created. The name of the Constructor is the same as the name of the class.

Characteristics of Constructor:-

  • Constructor do not have return type or value.
  • Constructor have same name as class name.
  • Constructor cannot be inherited .
  • java automatically calls the Constructor when an object is created.

Types Of Constructor:-

  1. Simple/default Constructor
  2. Parameterized Constructor

Simple/Default Java Constructor:-


Output:-
Creating Sample

Parameterized Java Constructor:-


Output:-
Creating Sample 0
Creating Sample 1
Creating Sample 2
Creating Sample 3
Creating Sample 4

No comments:

Post a Comment