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