0% found this document useful (0 votes)
5 views3 pages

Java Constructor Overloading Example

The document provides a Java program demonstrating constructor overloading using the 'this' keyword. It includes a default constructor that initializes 'rollNum' to 100 and a parameterized constructor that adds a given number to 'rollNum'. The main method creates an instance of the class and prints the final value of 'rollNum', which is 112.

Uploaded by

a70497944
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)
5 views3 pages

Java Constructor Overloading Example

The document provides a Java program demonstrating constructor overloading using the 'this' keyword. It includes a default constructor that initializes 'rollNum' to 100 and a parameterized constructor that adds a given number to 'rollNum'. The main method creates an instance of the class and prints the final value of 'rollNum', which is 112.

Uploaded by

a70497944
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

DEPARTMENT OF COMPUTER SCIENCE AND DESIGN

3.b. Write a program in Java to demonstrate constructor overloading using this keyword.

public class OverloadingExample2

private int rollNum;

OverloadingExample2()

rollNum =100;

OverloadingExample2(int rnum)

this();

/*this() is used for calling the default

* constructor from parameterized constructor.


DEPARTMENT OF COMPUTER SCIENCE AND DESIGN

* It should always be the first statement

* inside constructor body.

*/

rollNum = rollNum+ rnum;

public int getRollNum()

return rollNum;

public void setRollNum(int rollNum)

[Link] = rollNum;

}
DEPARTMENT OF COMPUTER SCIENCE AND DESIGN

public static void main(String args[])

OverloadingExample2 obj = new OverloadingExample2(12);

[Link]([Link]());

Output:

112

You might also like