Programs Using Constructors

CONSTRUCTORS

Constructors:
                            A Constructor is same as a class name without any return type  and with a prototype. A constructor is used for initializing the variables. 
                 
Constructors are as follows:
  1. Default Constructor.
  2. Parameterized Constructor.

1.Default constructor:

Default constructor is used to initialize the variables with the values given at compile time.
Even though we create or not there is default constructor for a program .

Program using Default constructor

class DefaultConstructor 

{

int rollNum;

String name;

DefaultConstructor()

{

rollNum=1330;

name="Void Main Technologies";

}

public static void main(String[] args) 

{

DefaultConstructor dc=new DefaultConstructor();

System.out.println("Roll number="+dc.rollNum+"\n"+"Name="+dc.name);

}

}



Save As: 

DefaultConstructor .java

Compile:


javac DefaultConstructor.java

Run:

java DefaultConstructor

Output:

Roll Number=1330
Name= void Main Technologies                        

Note:




2.Parameterized constructor:

A constructor with parameters can be called as Parameterized Constructor.

In these the values are passed when we create an object for the class.

Parameters of the object (actual parameters) are passed to the parameters of the constructor(formal parameter) 

Program using Parameterized constructor

class ParameterizedConstructor
{
int rollNum;
String name;


ParameterizedConstructor(int a,String b)
{
rollNum=a;
name=b;
}
public static void main(String[] args)
{
ParameterizedConstructor pc=new ParameterizedConstructor(1330,"Void Main Technologies");
System.out.println("Roll number="+pc.rollNum+"\n"+"Name="+pc.name);
}
}

Save As: 

ParameterizedConstructor .java


Compile:


javac ParameterizedConstructor .java

Run:


java ParameterizedConstructor


Output:

Roll Number=1330
Name=void Main Technologies           

Note: 

if we want to pass different values for every time then we have to change the code always.So This can be avoided by using the Parameterized Constructor at Run Time. 

Program using Parameterized constructor at Run time

import java.util.Scanner;

class ParameterizedConstructor
{
int rollNum;
String name;
ParameterizedConstructor(int a,String b)
{
rollNum=a;
name=b;
}
public static void main(String[] args)
{
System.out.println("enter roll number and name");

Scanner sc=new Scanner(System.in);

int roll=sc.nextInt();
String name=sc.next();

ParameterizedConstructor pc=new ParameterizedConstructor(roll,name);

System.out.println("Roll number="+pc.rollNum+"\n"+"Name="+pc.name);
}
}




Save As: 


ParameterizedConstructor .java


Compile:



javac ParameterizedConstructor .java

Run:


java ParameterizedConstructor


Input :

enter roll number and Name

1330


  
Output:

Roll Number=1330
Name=void Main Technologies             


Note :

To pass the values at run time or from the keyboard the we must use Scanner class object.The values passed at run time can be passed as a parameters to the constructor as actual parameters .



Using More Number of Constructors:A single Class Can have any number of constructors but must differ in there Parameters list or type of parameters or order of parameters.


Program Using More Than one Constructor

import java.util.Scanner;

class ParameterizedConstructor
{
int rollNum;
String name;

ParameterizedConstructor()
{
rollNum=1330;
name="Void Main Technologies";
}
ParameterizedConstructor(int a,String b)
{
rollNum=a;
name=b;
}
public static void main(String[] args)
{

System.out.println("enter roll number and name");

Scanner sc=new Scanner(System.in);

int roll=sc.nextInt();
String name=sc.next();

ParameterizedConstructor pc=new ParameterizedConstructor(roll,name);

System.out.println("vales from default Constructor");

System.out.println("Roll number="+pc.rollNum+"\n"+"Name="+pc.name);

System.out.println("vales from default Constructor");

ParameterizedConstructor pc1=new ParameterizedConstructor();

System.out.println("Roll number="+pc1.rollNum+"\n"+"Name="+pc1.name);
}
}


Save As: 

ParameterizedConstructor .java


Compile:



javac ParameterizedConstructor .java

Run:


java ParameterizedConstructor


Input :

enter roll number and Name

1330
void Main Technologies

Output:

Values From Parameterized Constructor
Roll Number=1330
Name=void Main Technologies                            



Values From default Constructor
Roll Number=1330

Name=void Main Technologies                




Comments

Popular posts from this blog

Programs on Inheritance

Arrays In Java

Java Inner Classes