Constructor use to create the object and for instantiation purpose

Posted by Gyan Guide on April 7th, 2017

Constructor is a special type of method which holds the all properties of method but mainly constructor is used to create the object.

Java constructor is called at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.

Rules to create the constructor:

1) It always holds the class name ,means constructor name should be same as the class name.

2) It does not have any return type or return statement.

3) It has access specifier

4) There is no need to explicitly define the constructor , once we create the object it automatically calls. Advance java training in bangalore

5)It always constructs the values for the object ,so it is known as constructor.

Types of java constructors

There are two types of constructors in java:

  1. Default constructor (no-arg constructor)

  2. Parameterized constructor

Default Constructor : When the constructor is not having any argument is known as default constructor.

Example :

class Demo

{

    public Demo()

         {

              System.out .println(“Running zero argument constructor”);

        }

   public static void main (String [] args)

       {

             Demo obj = new Demo(); //create the object

        }

}

Output : Running zero argument constructor

Explanation : In above given example we can see that we are defining a constructor ,once we create the object automatically constructor calls and we get the output,we doesn’t need to call the constructor implicitly.

Java course in bangalore

Need Of Default Constructor :

Default constructor provides the default values to the object like 0, null etc. depending on the type of data.

Note : If there is no constructor in a class, compiler automatically creates a default constructor.

Parameterized Constructor : When the constructor is having arguments is known as parameterized constructor.

Example

class Run

{

   public Run (int a)

     {

    System.out .println(“Running one argument constructor”);

    }

     public Run (int a,double b)

      {

        System.out .println(“Running two argument constructor”);

      }

 public Run (String name)

      {

         System.out .println(“Running string argument constructor”);

       }

    public static void main (String [] args)

         {

              Run obj = new Run(34); //create the object

              Run obj = new Run(34,87.9);

               Run obj = new Run(“Ram”);

         }

}

Output :

Running one argument constructor

Running two argument constructor

Running string argument constructor

java training center in bangalore

Explanation : Once we create the object and pass the parameters inside the constructor we get the output on the basis of the passed parameters.

Need Of Parameterized Constructor :

Parameterized constructor is used to provide different values to the different objects.

Benefits of constructor:

1) To instantiate something that mean if we want something to happen automatically that time we have to create the object.

2) To create the object.

3) Provides all behaviour similar to method like code reusability.

About Constructor :

1.Every class has a constructor whether it’s normal one or a abstract class(having abstract methods).

2.constructor are not methods and they don’t have any return type.

Best core java training in bangalore

3.Constructor can use any access specifier, they can be declared as private also. Private constructors are possible in java but there scope is within the class only.

 

4.Like constructors method can also have name same as class name, but still they have return type, though which we can identify them that they are methods not constructors.

 

5.If you don’t define any constructor within the class, compiler will do it and it will create a constructor known as default constructor.

 

6.Constructor overloading is possible but overriding is not possible. Which means we can have overloaded constructor in our class but we can’t override a constructor.

java/j2ee classes bangalore

7.Constructors can not be inherited, Because it doesn’t having access modifier. Constructor neither static nor nonstatic. We can inherit the only nonstatic members.

 

8.If Super class doesn’t have a no-arg(default) constructor then compiler would not define a default one in child class as it does in normal scenario.

 

9.Interfaces do not have constructors.

10.Abstract class can have constructors and these will get called when a class, which implements the abstract methods ,gets instantiated. (i.e. object creation of concrete class).

This is the short description about the constructor to know more about the java attend classes in Infocampus Software Training Institute with the 100% placement support.

Author :

Infocampus provides Advance java training in bangalore with 100% job support.3 days free demo classes are available for Core and Advance java .

Pay fees in installaments on java course .Syllabus is according to easily understandable by the learners . Java training is provided on the weekdays and weekends.

Learn OOPs concept,Packages,Threads, servlet,hibernate,spring etc .

To get the free demo classes on best core java training call at : 9738001024 or for more details on java/j2ee classes

enquire at: http://www.infocampus.co.in/java-training-bangalore.html

Like it? Share it!


Gyan Guide

About the Author

Gyan Guide
Joined: February 8th, 2017
Articles Posted: 35

More by this author