Written by 3:26 PM Masters of Computer Science, Object Oriented Programming

Types of Constructor in Java | Chain of Constructor

Tell Your Friends

Know about types of constructor, destructor, finalize method in Java along with the chain of the constructor.

Tell Your Friends

Last Updated on by ICT Byte

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
Visited 4 times, 1 visit(s) today
[mc4wp_form id="5878"]
Close