Write a java program which has various types of constructor and overload these constructors when the object is being created of that class.
Write a java program which has various types of constructor and overload these constructors when the object is being created of that class.
class variousConstructor {
public static void main(String[] args) {
constructor c1 = new constructor();
constructor c2 = new constructor(23);
constructor c3 = new constructor(45.6);
}
}
class constructor {
public constructor() {
System.out.println("It's 1st constractor.");
}
public constructor(int n1) {
System.out.println("It's constractor with integer variable which is " + n1);
}
public constructor(double d1) {
System.out.println("it's a constractor with double variable which is " + d1);
}
public constructor(float f1) {
System.out.println("It's a constractor with float variable which is " + f1);
}
}
OUTPUT:
It's 1st constractor.
it's a constractor with double variable which is 23
It's a constractor with float variable which is 45.6
Comments
Post a Comment
Please do not enter any spam link in the comment box.