Masters of Computer Science Object Oriented Programming

Types of Constructor in Java | Chain of Constructor

Types of Constructor in Java | Chain of Constructor

Types of Constructor

  • Default Constructor
    • Doesn’t have any parameter
    • Example

Class rectangle{

rectangle(){

System.out.println(“Default Constructor”);

  • No argument constructor
  • Parameterized Constructor
    • Has parameter
    • When object is initialized, parameters are taken
    • Example

Class rectangle{

Int length, breadth;

rectangle(int l, int b){

length=l;

breadth=b;

}

int area(){

Destructor

  • There is no destructor in java as of C++
  • There is automatic garbage collector – it frees the dynamically allocated memory when there is no use

Finalize() Methods

  • Works like destructor
  • Opposite of constructor
  • Finalize method is used – for removing object

Chain of Constructor

  • Process of calling constructor from another constructor
  • Can be done in two ways
    • Within same class: Using this() keyword
    • From base class : Using super() keyword
  • It goes with inheritance
About Author

ICT Byte