Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views6 pages

Understanding Java Constructors

Uploaded by

ABHYUDAY HARSH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Understanding Java Constructors

Uploaded by

ABHYUDAY HARSH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CONSTRUCTORS

* A “CONSTRUCTOR” is a special member-function of a class which has the


same name as the class.

* Constructors are used to initialize the data-members of the class with legal
values when an object of the class is created.

* Constructors do not have an explicit return-type; not even “void”.


Ex:
class Num void disp( )
{ {
int a, b, R ; System.out.println("a = " + a) ;
System.out.println("b = " + b) ;
// Default Constructor System.out.println("Result = " + R) ;
Num( ) }
{
a=b=R=0; public static void main(String
} args[ ])
{
// Parameterized Constructor Num ob1 = new Num(11, 25) ;
Num(int v1, int v2) Num ob2 = new Num( ) ;
{
a = v1 ; ob1.cal( ) ;
b = v2 ; ob2.cal( ) ;
R=0;
} System.out.println("1st Object :- ") ;
ob1.disp( ) ;
void cal( )
{ System.out.println("2nd Object :-
R = (a + b) * (b – a) ; ") ;
} ob2.disp( ) ;

}}

Num ob1 = new Num(11, 25) ;


Num ob2 = new Num( ) ;

Or

Num ob1 = new Num(11, 25), ob2 = new Num( ) ;


class School
{
int x, y, R ;

// Default Constructor
Num( )
{
x=y=1;
R=0;
}

// Parameterized Constructor
Num(int v)
{
x=v;
y=1;
R=0;
}

// Parameterized Constructor
Num(int v1, int v2)
{
x = v1 ;
y = v2 ;
R=0;
}

void cal( )
{
R = (x * x * x) – (y * y) ;
}

void disp( )
{
System.out.println("x = " + x) ;
System.out.println("y = " + y) ;
System.out.println("Result = " + R) ;
}
public static void main(String args[ ])
{
School ob1 = new School(5) ;
School ob2 = new School( ) ;
School ob3 = new School(7, 3) ;

ob1.cal( ) ;
ob2.cal( ) ;
ob3.cal( ) ;

System.out.println("1st Object :- ") ;


ob1.disp( ) ;

System.out.println("2nd Object :- ") ;


ob2.disp( ) ;

System.out.println("3rd Object :- ") ;


ob3.disp( ) ;

}}
Types of Constructors

i> Default Constructors : A Default Constructor is a constructor which does


not have any arguments.
It is used to initialize the data-members of the class with fixed, pre-
determined values during object creation.
Default Constructors are also referred-to as zero-argument
constructors.

ii> Parameterized Constructors : A Parameterized Constructor is a


constructor which has one or more arguments.
It is used to initialize the data-members of a class with user-supplied
values during object creation.
Special Features of Constructors

i> A constructor has the same name as it’s class.

ii> Constructors do not have an explicit return-type; not even “void”.

iii> The implicit return-type of a constructor is an object of it’s class.

iv> Constructors can be overloaded.

v> A constructor is automatically invoked by the JVM whenever an object of


the class is created.

vi> If no constructors have been defined for a class then Java automatically
provides a default constructor for that class.

You might also like