Parameterized Constructor: The constructor which take argument from the user are said to be parameterized constructor.
The general form
is ,
Classname(argument
list)
{
body
of the constructr
}
PROGRAM
class Sample
{
int i;
Sample( )
//default constructor
{
i=0;
}
Sample( int a )//parameterized
constructor
{
i=a;
}
void show( )
{
System.out.println("i="+i);
}
}
class ConsDemo2
{
public
static void main(String args[ ])
{
Sample
ob1=new Sample( );
ob1.show(
);
Sample
ob2=new Sample(5);
ob2.show(
);
}
}
Comments
Post a Comment