Constructor and its type in Java

Ā 
Video Tutorials
Artificial Intelligence Tutorial
Machine Learning Projects TutorialPython Video tutorial
Computer Graphics Video TutorialC++ Video Tutorial
Big Data Analytics Video TutorialSoft Computing Video Tutorial
Placement Video TutorialJava Video Tutorial

What is a Constructor in Java? Explain types of Constructors in Java – Java Tutorial

Problem Statement

What are constructors in Java? Explain its types with suitable programming examples.

OR

Can you overload the constructor and destructor? Justify with a suitable program.

Solution:

Constructor is a special method that is used to initialize the values of instance variables at the time of the creation of objects.

Video tutorial on Constructor and its types in Java

Features of constructor in Java:

The constructor will have the same name as that of the class name.

It does not specify a return type not even void.

It should be declared in the public section.

Types of Constructors in Java

1. Default constructor: It is a constructor which does not take any argument.

2. Parameterized constructor: It is a constructor which takes any number of parameters.

A default constructor is automatically loaded by the compiler.

Example program to demonstrate the default constructor

class B
{
   int x;
   B( )
   {
         System.out.println("Constructing Box");
          x = 10;
   }
   void show( )
   {
	System.out.println("x = " + x);
   }
}

class MB 
{
     public static void main(String args[])
     {
          B b1 = new B( );
          b1.show( );
      }
}

Output:

Constructing Box

x = 10

Example program to demonstrate the parameterized constructor

class B
{
     int x, y;
     B(int a, int b)
    {
           x = a;
           y = b;
     }
     void show( )
     {
          System.out.println("x = " + x);
          System.out.println("y = " + y);
     }
}
class MB 
{
     public static void main(String args[]) 
     {
           B b1 = new B(4, 5);
           b1.show( );
     }
}

Output:

x = 4

y = 5

Constructor Overloading in java

Overloading is one of the important concepts in Object Oriented Programming.

Similar to methods the constructors can also be overloaded.

Constructor overloading in Java allows having more than one constructor inside one Class.

Multiple constructors with different signatures with the only difference is that Constructor doesn’t have a return type in Java.

Those constructors will be called the overloaded constructors.

Example program to demonstrate the Constructor Overloading

class B
{
      int x, y;
      B( )
      {
            x = 10;
            y = 20;
      }
      B(int a, int b)
      {
            x = a;
            y = b;
      }
      void show( )
      {
            System.out.println("x = " + x);
            System.out.println("y = " + y);
      }
}

class MB 
{
     public static void main(String args[]) 
     {
          B b1 = new B(4, 5);
          b1.show( );
          B b2 = new B();
          b2.show( );
      }
}

Output:

x = 4

y = 5

x = 10

y = 20

Why does Java not support destructors?

The destructor is used to free or deallocate the memory of unused variables.

This is called cleaning up.

Java has an in-built mechanism for cleaning up.

This mechanism is called Garbage collection.

The garbage collector automatically deallocates the memory of unused variables.

Hence there is no need for destructors in Java.

How does the finalize method help in garbage collection

Finalization is the facility provided by Java for the classes for cleaning up the native resources before the objects are garbage collected.

The garbage collector is unable to control the cleaning up of native resources which are used earlier.

Then the responsibility of cleaning up those native allocations falls on the object’s finalization code.

Thus the purpose of finalization is to clean up the native resources used earlier. The finalize() method must be run before invoking the garbage collector.

Summary:

In this article, we understood What are constructors in Java? Explain its types with suitable programming examples – Java Tutorial. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and the YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *