0% found this document useful (0 votes)
220 views8 pages

Java - Constructor Chaining With Example

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

Java - Constructor Chaining With Example

informatika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

16/9/2015

JavaConstructorChainingwithexample

Beginner'sBook

CoreJava
OOPsConcepts
JavaCollections
JSP
JSTL
InterviewQuestions
JSPInterviewQ
JUnitInterviewQ
ServletInterviewQ

JavaConstructorChainingwithexample
OOPsConcept
[Link]
willunderstandthiswiththehelpofbelowprogram.

ExampleProgram
InthebelowexampletheclassChainingDemohas4constructorsandwearecallingoneconstructor
fromanotherusingthis()[Link]
havesuppliedastringinthis()statementlikethis(hello).
Note:this()shouldalwaysbethefirststatementinconstructorotherwiseyouwillgetthebelowerror
message:
Exceptioninthread"main"[Link]:Unresolvedcompilationproblem:
Constructorcallmustbethefirststatementinaconstructor

CompleteCode:
[Link];
publicclassChainingDemo{
//defaultconstructoroftheclass
publicChainingDemo(){
[Link]("Defaultconstructor");
}
publicChainingDemo(Stringstr){
this();

[Link]("Parametrizedconstructorwithsingleparam");
}
publicChainingDemo(Stringstr,intnum){
//ItwillcalltheconstructorwithStringargument

this("Hello");

[Link]("Parametrizedconstructorwithdoubleargs");
}
publicChainingDemo(intnum1,intnum2,intnum3){

//Itwillcalltheconstructorwith(String,integer)arguments
this("Hello",2);
[Link]("Parametrizedconstructorwiththreeargs");
[Link]

1/8

16/9/2015

JavaConstructorChainingwithexample

}
publicstaticvoidmain(Stringargs[]){
//CreatinganobjectusingConstructorwith3intarguments
ChainingDemoobj=newChainingDemo(5,5,15);
}
}

Output:
Defaultconstructor
Parametrizedconstructorwithsingleparam
Parametrizedconstructorwithdoubleargs
Parametrizedconstructorwiththreeargs

YouMightLike:
JavaDefaultconstructorwithexample
ConstructorOverloadinginJavawithexamples
Javaparameterizedconstructorwithexample
JavastaticconstructorIsitreallyPossibletohavetheminJava?
JavaConstructorinInterface?
ConstructorsinJavaAcompletestudy!!
Javaprivateconstructorexample
chainingconstructor
PreviousArticleNextArticle
2commentsaddone
SeanOctober22,2014,9:26AM
Butwhatisthepointofconstructorchaining?
[Link]?
Reply
KahmaliNovember28,2014,4:10PM
[Link]

2/8

16/9/2015

JavaConstructorChainingwithexample

[Link]
[Link]
throughabunchofdifferentconstructors,butonlyhavetheinitializationdoneinasingle
[Link],while
[Link],andtwodifferentconstructors
requireaspecificparameter,youwillhavetoinitializethatparamtwice,andwhenthe
initializationchanges,youllhavetochangeitineveryconstructor,insteadofjusttheone.
Reply
LeaveaComment
Name*
Email*
Website

Comment
Submit

ConfirmyouareahumanBeing
Notifymeoffollowupcommentsviaemail
OOPsConcepts
Methodoverloading
Methodoverriding
OverloadingvsOverriding
Constructor
Defaultconstructor
Parameterizedconstructor
Privateconstructor
Constructorininterface
Constructorchaining
staticconstructor
Constructoroverloading
Aggregation
Association
OOPsbasics
Polymorphism
Typesofpolymorphism
Inheritance
Typesofinheritance
[Link]

3/8

16/9/2015

JavaConstructorChainingwithexample

Hybridinheritance
Hierarchicalinheritance
Multilevelinheritance
Multipleinheritance
Encapsulation
Staticanddynamicbinding
Staticclass
Staticmethods
Staticvariables
Staticimport
Staticconstructor
Innerclasses
Abstractclassandmethods
Interface
Abstractclassvsinterface
Accessmodifiers
Packages
MORE...
Stringhandling
Exceptionhandling
Multithreading
JavaI/OTutorial
JavaSerialization
RecentlyAdded..
JSONTutorial
JavaRegularExpressionsTutorial
JavaEnumTutorial
JavaAnnotationsTutorial

[Link]

4/8

16/9/2015

JavaConstructorChainingwithexample

[Link]

5/8

16/9/2015

JavaConstructorChainingwithexample

[Link]
LikePage

5.8klikes

Bethefirstofyourfriendstolikethis

FollowMeonGoogle+
Chaitanya Singh
Ikuti
1.724 pengikut

JoinUsOnGooglePlus
BeginnersBook
Ikuti

+1

+ 2.568

[Link]

6/8

16/9/2015

JavaConstructorChainingwithexample

ReferenceLinks
[Link]

7/8

16/9/2015

JavaConstructorChainingwithexample

DownloadJava
EclipseIDEDownloads
JavaDocumentation
JavaEE5Tutorial
JavaEE6Tutorial
JavaEE7Tutorial
JavaSE6.0APISpecification
JavaSE7.0APISpecification
JavaServerPagesJSP
PopularTutorials
CoreJavaTutorial
JSPTutorial
JSTLTutorial
JavaCollectionsTutorial
ServletTutorial
CTutorial
JavaStringTutorial
Friends&Links
[Link]
DZoneFreshLinks
MostVisited
JavaInterview
JSPInterview
JavaMultithreadingInterview
JavaCollectionsInterview
ServletInterview
JUnitInterview
AboutChaitanyaSingh

[Link],[Link]
techblogwherehesharestutorialsonprogramming(Java,C,CPP),WordPress,SEOandweb
development.
Connecton
Copyright20122015BeginnersBookAllRightsReserved||Sitemap

[Link]

8/8

You might also like