Friday, May 29, 2015

Param.. Constructor

package package1;

//PARAMATERIZED CONSTRUCTOR

public class Class1 {

String name;
String add;

// Parameterized Constructor use GARYO bhana
// No need to initialized the variables in saperate line
// If there are lot of variables , and
//If we use the parameterized constructor we can assign all the variables in the same line
// OVERLOADING: Different constructors with different parameters

public Class1(String name1 , String addd) {

name = name1;  // Variable name outside the constructor assign the given string name1
this.add = addd ; //  Both upper way and the lower way are same

}

public static void main(String[] args) {

Class1 c1 = new Class1("jva", "dev");
System.out.println(c1.name +"\n"+ c1.add);
}
}


Consistent Parameterized Constructor:




public class Demo {

public static void main(String[] args) {

Circle c = new Circle(4,5);
System.out.println("\n");
Circle cc = new Circle(99);
}
}

class Circle{

double radius;
double PI;

public Circle(double radiusVal,double piVal){
System.out.println("Parameterized COnstructor is called.. ");
radius = radiusVal;
PI =  piVal;
System.out.println(radius);
System.out.println(PI);
}

public Circle(double radi) {
this(radi , -1); // This will also called the upper one constructor
System.out.println("Constructor with single ARG is called..");
// System.out.println(radius);
// System.out.println(PI);
}
}


No comments:

Post a Comment