C# Corner
Tech
News
Videos
Forums
Trainings
Books
Live
More
Interviews
Events
Jobs
Learn
Career
Members
Blogs
Challenges
Certifications
Bounties
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Java - Constructor Program
WhatsApp
Senthilvelan Sambamoorthy
Jul 31
2016
550
0
0
class
cons
{
int
l;
int
b;
cons()
// default constructor
{ l=
1
;
b=
2
;
}
cons(
int
tl)
// function name is same as class name
{ l=tl;
b=
4
;
}
cons(
int
tl,
int
tb)
// class name same as function name with different parameter
{ l=tl;
b=tb;
}
void
display()
{
System.out.println(
"\n"
);
System.out.println(
"Length of the rectangle = "
+l);
System.out.println(
"Breadth of the rectangle = "
+b);
}
public
static
void
main(String arg[])
{
cons r1=
new
cons();
r1.display();
cons r2=
new
cons(
6
);
r2.display();
cons r3=
new
cons(
10
,
20
);
r3.display();
}
}
Constructor Program
Java
Up Next
Java - Constructor Program