0% found this document useful (0 votes)
97 views

Constructors

Uploaded by

api-254961130
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Constructors

Uploaded by

api-254961130
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Constructor :-

After Creation of Object compulsory we should perform initialization then only


that object is in a position to provide service to others.
At the time of Object Creation some piece of code will execute automatically to
perform initialization that piece of code is nothing but Constructor.
ence the main Objective of constructor is to perform initialization.
!f we don"t have constructor we will face burden. #or example see the following
code.
Ex:-
class $tudent
%
$tring name&
int rollno&
public static void main'$tring() args*
%
$tudent s+ , new $tudent'*&
$tudent s- , new $tudent'*&
$tudent s. , new $tudent'*&
$tudent s/ , new $tudent'*&
s+.name , 01alli0&s+.rollno , +2+&
s-.name , 0$an3ar0&s-.rollno , +2-&
s..name , 04iran0&s..rollno , +2.&
s/.name , 0$ai0&s/.rollno , +2/&
$ystem.out.println's+.name506666605s+.rollno*&
$ystem.out.println's-.name506666605s-.rollno*&
$ystem.out.println's..name506666605s..rollno*&
$ystem.out.println's/.name506666605s/.rollno*&
7
7
8o over come this type of burden constructor was introduced.
Ex:-
class $tudent
%
$tring name&
int rollno&
$tudent'$tring name9 int rollno*
%
this.name , name&
this.rollno , rollno&
7
public static void main'$tring() args*
%
$tudent s+ , new $tudent'0sai09+2+*&
$tudent s- , new $tudent'0raghav09+2-*&
$ystem.out.println's+.name506666605s+.rollno*&
$ystem.out.println's-.name506666605s-.rollno*&
7
7
output :-
sai66666+2+
raghav66666+2-
Rules for Constructor
:hile writing constructors we should follow the following rules.
+* 8he name of the constructor and name of the class must be same.
-* 8he only allowed modifiers for the constructors are public9 private9
protected9 ;default<. !f we are using any other modifier we will get
C.='Compiler =rror*.
Ex:
class 8est
%
static 8est'*
%
6666
7
7
C.=>6 modifier static not allowed here.
.* return type is not allowed for the constructors even void also. !f we r
declaring return type then the compiler treats it as a method and hence
there is no C.= and ?.='?untime=rror*.
class 8est
%
void 8est'*
%
$ystem.out.println'0ai .....0*&
7
public static void main'$tring arg()*
%
8est t , new 8est'*&
7
7
!t is legal'@ut $tupid* to have a method whose name is exactly same as class
name.
ere it was treated as method.
Default Constructor :-
!f we r not writing any constructor then the compiler always generate default
constructor.
!f we r writing at least one constructor then the compiler won"t generate any
constructor.
ence every class contains either programmer written constructor or compiler
generated default constructor but not both simultaneously.
Prototype of default constructor: -
+* !t is always no6arg constructor.
-* !t contains only one A line super();
8his is a call to superclass A constructor it is no6argument call only.
.* 8he modifier of the default constructor is same as class modifier'either
public or default*.
Brogrammers code Compiler generated code
class 8est
%
7
class 8est
%
8est'*
%
super'*&
7
7
public class 8est
%
7
public class 8est
%
public 8est'*
%
super'*&
7
7
class 8est
%
private 8est'*
%
7
7
class 8est
%
private 8est'*
%
super'*&
7
7
class 8est
%
void 8est'*
class 8est
%
void 8est'*
%
7
7
%
7
8est'*
%
super'*&
7
7
class 8est
%
8est'*
%
this'+2*&
7
8est'int i*
%
7
7
class 8est
%
8est'*
%
this'+2*&
7
8est'int i*
%
super'*&
7
7
8he first line inside any constructor must be a call to super class
constructor'super'** or a call to overloaded constructor of the same
class'this'**. !f we are not ta3ing any thing then the compiler will
always place super'*.
super() & this() in constructor :-
we should use as first statement in constructor. :e can use either super or this
but not both simultaneously. Outside constructs we can"t use i.e we can invo3e
a constructor directly from another constructor only.
Overloaded Constructor :-
A class can contain more than one constructors with different arguments. 8his
type of constructors
are called overloaded constructor.
Ex:-
Case1:
class 8est
%
8est'*
%
super'*&
$ystem.out.println'0constructor0*
7
7
Case:
class 8est
%
8est'*
%
$ystem.out.println'0constructor0*
super'*&
7
7
C.=> Call to super'* must be first statement in constructor.
Case!:
class 8est
%
8est'*
%
super'*&
this'+2*&
super'0Constructor0*&
7
8est'int i*
%
$ystem.out.println'0Constructor0*
7
7
C.= >6 cannot use super and this simultaneously in a constructor.
Case" :-
class Test
{
public void m1()
{
super();
}
}
C.=> Call to to this must be first statement in a constructor.
Overloaded Constructor :-
A class can contain more than one constructors with different arguments. 8his
type of constructors
are called overloaded constructor.
Ex:-
class 8est
%
8est'*
%
this'+2*&
$ystem.out.println'0Co6arg constructor0*&
7
8est'int i*
%
this'+2.D*&
$ystem.out.println'0int6arg0*&
7
8est'double d*
%
$ystem.out.println'0double6arg0*
7
public static void main'$tring arg()*
%
8est t+ , new 8est'*&
8est t- , new 8est'+2*&
8est t. , new 8est'-2.D*&
8est t/ , new 8est'EaE*&
8est tD , new 8est'+2l*&
7
7
!nheritance concept is not applicable for constructor and hence overriding is
also not applicable.
Ex 1 :-
class 8est
%
public static void m+'*
%
$ystem.out.println'0m+ method0*&
m-'*&
7
public static void m-'*
%
$ystem.out.println'0m- method0*&
m+'*&
7
public static void main'$tring arg()*
%
m+'*&
$ystem.out.println'0ello......hai0*&
7
7
?.= > 6 ?ecursive method invo3ation leads to runtime exception which is $tac3
over flow.
Ex 2:-
class Test
%
8est'*
%
this'+2*&
7
8est'int i*
%
this'*&
7
public static void main'$tring arg()*
%
$ystem.out.println'0ai........hello0*&
7
7
C.= > 6 ?ecursive constructor invocation leads to compile time error.
=F>6
class p %
7
class c extends p %
7
8he above declaration is valid.
=x>6
class p %
p'* %
7
7
class c extends p %
7
8he above declaration is also valid.
=x>6
class p
%
p'int i*
%
7
7
class c extends p
%
7
C.= > 6 @ecause the super call of sub class constructor calls the super class
constructor. $o in the above example there is no super class constructor ie is
why we are getting compile time error.
when ever we r writing any constructor. !t is remanded to provide no6arument
constructor also. !f the parent class contains some constructors then while writing
child class constructor we should ta3e a bit care.
!f the parent class constructor throws some checked exception. Compulsory the
child class constructor should throw the same chec3ed exception or it"s parent
exception other wise compile time error.
=x>6
class p
%
7
class c extends p
%
7
=x>6
class p
%
p'*
%
7
7
class c extends p
%
7
=x>6
class p
%
p'* throws !O=xception
%
7
7
class c extends p
%
c'*
%
super'*&
7
7
C.= > 6 @ecause the parent class constructor is throwing the chec3ed exception the
child class constructor must also throw the same exception or its parent type.
!f the parent class constructor throws unchecked exception then child class
constructor not reGuired to throw that exception.
=x>
class p
%
p'* throws Arithmetic=xception
%
7
7
class c extends p
%
c'*
%
super'*&
7
7
#hich of the follo$in% state&ents are true'
+. 8he first line inside constructor either super'* or this'* but not both
simultaneously. HH it is correct
-. !f we are not ta3ing either super'* or this'* as the first line in the
constructor then the compiler will always place this'*. HH it is not correct.
.. Compiler will always generate default constructor. HH it is not correct.
/. Constructor overriding is possible but overloading is not possible. HH it is not
correct.
D. =ven for constructor also inheritance possible. HH it is not correct.
I. !f the parent class constructor throws some chec3ed exception compulsory
child class constructor should throw same exception or its parent
exception. HH it is correct.

You might also like