Constructor in Java
Constructor in Java
Constructor
class class_name
{
class_name( )
{
body of constructor
}
}
Characterstics:-
must have the same name as class name
method does not have any return_type not even void
used to initialized the instance variables of an object
types are default constructor and parameterized constructor
compiler creates a default constructor if we does not define any
constructor
it can be overloaded
use this( ) to communicate from one constructor to another
constructor in the same class
use super( ) to communicate with super class
Default Constructor
import java.util.*;
class Compute
{
Scanner sc = new Scanner(System.in);
int a,b;
Compute( )
{
System.out.println(“Enter the value of a and b: ”);
a=sc.nextInt( );
b=sc.nextInt( );
}
void display( )
{
System.out.println(“You Entered:- ”);
System.out.println(“a = ”+a+“ b = ”+b);
}
}
class MCompute
{
public static void main(String args[])
{
Compute C=new Compute( );
C.display( );
}
}
Parameterised constructor
class class_name
{
variable declaration
class_name(arg1,arg2,…,argN);
method declaration
}
class main_class_name
{
public static void main(String args[])
{
class_name object_name = new class_name(val1,val2,…valN);
}
}
example:-
class Compute
{
int a,b;
Compute( int x, int y)
{
a=x;
b=y;
}
void display( )
{
System.out.println(“Your value:-”);
System.out.println(“a = ”+a+“ b = ”+b);
}
}
class MCompute
{
public static void main(String args[])
{
Compute C=new Compute(11,12 );
C.display( );
}
}
class Area
{
int A;
Area(int s)
{
A=s*s;
System.out.println(“Side of square = ”+s);
System.out.println(“Area of square = ”+A);
}
Area(int l, int b)
{
A=l*b;
System.out.println(“Length of rectangle = ”+l);
System.out.println(“Breadth of rectangle = ”+b);
System.out.println(“Area of Rectangle = ”+A);
}
}
class MArea
{
public static void main(String args[])
{
Area obj1 = new Area(10);
Area obj2 = new Area(5,6);
}
}
use of this( )
class Val
{
Val( )
{
System.out.println(“This is default constructor”);
}
Val( int v1)
{
this( );
System.out.println(“ single parameterized constructor”);
System.out.println(“v1 = ”+v1);
}
Val( int v1, int v2)
{
this( v1);
System.out.println(“two parameterized constructor”);
System.out.println(“v1 = ”+v1+ “ v2 = ”+v2);
}
}
class MVal
{
public static void main(String args[])
{
Val v = new Val(40,10);
}
}
Modifiers
Access Specifier
o private
o public
o protected
o default
no access specifier
o static
o final
Method overloading
class class_name
{
return_type method_name(arg1)
{
-----------
}
return_type method_name(arg1, arg2,..)
{
----------
}
}
example:-
class ABC
{
int A;
void area(int s)
{
A=s*s;
System.out.println(“Side of square = ”+s);
System.out.println(“Area of square = ”+A);
}
void area(int l, int b)
{
A=l*b;
System.out.println(“Length of rectangle = ”+l);
System.out.println(“Breadth of rectangle = ”+b);
System.out.println(“Area of Rectangle = ”+A);
}
}
class MABC
{
public static void main(String args[])
{
ABC obj = new ABC( );
obj.area(10);
obj.area(10,20);
}
}
example2:-
class xyz
{
int g;
void greatest(int a, int b)
{
g=a>b?a:b;
System.out.println(“a = ”+a+“b = ”+b);
System.out.println(“Greatest=”+g);
}
void greatest(int x, int y, int z)
{
g=x>y?x:y;
g=g>z?g:z;
System.out.println(“x = ”+x+“y = ”+y+“z = ”+z);
System.out.println(“Greatest=”+g);
}
}
class Abc
{
public static void main(String args[])
{
xyz ob = new xyz( );
ob.greatest(5,6);
ob.greatest(7,5,6);
}
}