Explan various types of constructors in Java


Constructor is a special member function whose task is to initialize the objects.

Constructor is a special members function which will be called automatically whenever an object is created for placing user defined values instead of placing default values.


The basic purpose of construction is that to initialize the data members of the object.


Constructors have the same name of the class itself.


Constructors don’t have any return type even void.


Advantages :

It eliminates in placing default values.


It eliminates in calling ordinary functions.


Java doesn’t support the concept of distructors.

Rules : Constructor called automatically whenever an object is create.


Constructor name must be similar to class name.


Constructor shouldn’t return any value.


Constructor shouldn’t be static.


It will not be inherited.



Ex:-

class Sample
{
int m;
int n;
Sample()
{
m=0;
n=0;
}

When a class contains a constructor like the one defined above. It is guaranteed that an object created by the class will be initialized automatically. For example, the declaration.

Sample x = new Sample()

Not only creates the object x of type sample but also initializes its data members m and n to zero.

Types of constructors : In java we have 2 types of constructors.

Default / Parameter less / No argument constructors.


Parameterised constructor.


1. Default constructor: - A constructor that accepts no parameters is called the default constructor for class A is A::A() If no such constructor is defined, then the compiler supplies a default constructor. Therefore a statement such as:

A a;

2. Parameterized Constructors: - It may be necessary to initialize the various data elements of different objects with different values when they are created. Java permits us to achieve his objective by passing arguments to the constructor function when the objects are created. The constructors that can take arguments are called parameterized constructors. 

class Sample
{
int m;
int n;
Sample(int x, int y)
{
m=x;
n=y;
}
}
Sample x =new Sample(10,20);

Multiple constructors in a class: - Java permits us to number of constructors in the same class. For example, could define a class as follows:

class Sample
{
int m;
int n;

Sample()
{
m=0;
n=0;
}

Sample(int a, int b)
{
m=a;
n=b;
}

Sample(Sample ob)
{
m=ob.m;
n=ob.n;
}

}
This declares three constructors for a sample object. The 1St constructor receives no arguments, the second receives 2 integer arguments and the third receives one sample class object as argument. For example, the declaration

Sample x = new Sample();

Sample x = new Sample(10,20);

Sample x = new Sample(sample ob);

Subclass Constructor: - 

A subclass constructor is used to construct the instance variables of both the subclass and the super class. The subclass constructor uses keyword super to invoke the constructor method of the super class The keyword supper is used subject to the following conditions.

·         Super may only be used within a subclass constructor method.

·    The call to super class constructor must appear as the 1St statement within the sub class constructor.

·     The parameters in the super call must match the order and type of the instance variable declared in the super class.

Related

Write about Decission making & looping in Java With Example?

Decision making & looping:                                               &...

Write about Jumps in loops in java

Jumps in loops: Break : [jumping of out of a loop] Break is used to come out of a loop, an early exists from a loop can be accomplished by using break statement. The break statement can be use...

Write about Labeled loops in Java

Labeled loops: In java we can give a label to a block of statements a label is any valid java variable name. to give a label to a loop, place it before the loop with a colon(:) at the end. For...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item