Home
| | Sitemap
||Page number :13
Constructor Overloading---
The basic concept underneath “Constructor Overloading” is same as that of method overloading. The difference lies on the concept that here we are working with constructors and not with methods. Lets see our next example of the apply series.
class overload { int n=0,n2=0;
public overload() { n=1;
n2=2; System.out.println("n & n2 "+n+" "+n2); }
public overload(int x) { n=x; System.out.println("n & n2 "+n+" "+n2); }
public overload(int x,int y) { n=x; n2=y; System.out.println("n & n2 "+n+" "+n2); } }
class apply12 { public static void main(String args[]) {
overload obj=new overload();
overload obj1=new overload(5);
overload obj2=new overload(10,15);
} }
Exercises
Q1.What is a constructor?
Q2.How does a constructor differ from a method?
Q3.Explain the two types of constructors that can be created by Java.
Q4.What is the general use of constructor?
Q5.Differentiate between constructor and method overloading”.
page 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29