CONSTRUCTOR OVERLOADING
***********************
1. Constructor overloading example 1
************************************
package programmesofcorejava;
class ConstructorOverloading
{
public static void main(String[] args)
{
Car c = new Car();
// c ==> object reference of Car type
[Link](" ############ CAR DETAILS ###########");
[Link](" The brand of the car is : "+[Link]);
[Link](" The color of the car is : "+[Link]);
[Link](" The no of wheels in the car is : "+[Link]);
[Link](" The fuel type of the car is : "+[Link]);
[Link](" The price of the car is : "+[Link]);
[Link](" ####################################");
Car c1 = new Car("HYUNDAI","BLUE",4,"PETROL",290000);
// c1 ==> object reference of Car type
[Link](" ############ CAR DETAILS ###########");
[Link](" The brand of the car is : "+[Link]);
[Link](" The color of the car is : "+[Link]);
[Link](" The no of wheels in the car is : "+[Link]);
[Link](" The fuel type of the car is : "+[Link]);
[Link](" The price of the car is : "+[Link]);
[Link](" ####################################");
}
}
class Car
{
String brand;
String color;
int noofwheels;
String type;
double price;
// no argument constructor
Car()
{
// parameterized constructor
Car(String brand, String color, int noofwheels, String type, double price)
{
[Link] = brand;
[Link] = color;
[Link] = noofwheels;
[Link] = type;
[Link] = price;
}
}
Output:
############ CAR DETAILS ###########
The brand of the car is : null
The color of the car is : null
The no of wheels in the car is : 0
The fuel type of the car is : null
The price of the car is : 0.0
####################################
############ CAR DETAILS ###########
The brand of the car is : HYUNDAI
The color of the car is : BLUE
The no of wheels in the car is : 4
The fuel type of the car is : PETROL
The price of the car is : 290000.0
####################################
2. Constructor overloading example2
***********************************
package programmesofcorejava;
class ConstructorOverloading
{
public static void main(String[] args)
{
Car c = new Car("HYUNDAI");
// c ==> object reference of Car type
[Link](" ############ CAR DETAILS ###########");
[Link](" The brand of the car is : "+[Link]);
[Link](" The color of the car is : "+[Link]);
[Link](" The no of wheels in the car is : "+[Link]);
[Link](" The fuel type of the car is : "+[Link]);
[Link](" The price of the car is : "+[Link]);
[Link](" ####################################");
Car c1 = new Car("HYUNDAI","BLUE",4,"PETROL",290000);
// c1 ==> object reference of Car type
[Link](" ############ CAR DETAILS ###########");
[Link](" The brand of the car is : "+[Link]);
[Link](" The color of the car is : "+[Link]);
[Link](" The no of wheels in the car is : "+[Link]);
[Link](" The fuel type of the car is : "+[Link]);
[Link](" The price of the car is : "+[Link]);
[Link](" ####################################");
}
}
class Car
{
String brand;
String color;
int noofwheels;
String type;
double price;
// no argument constructor
Car()
{
Car(String brand)
{
[Link] = brand;
}
Car(String brand, String color)
{
[Link] = brand;
[Link] = color;
}
Car(int noofwheels, String type, double price)
{
[Link] = noofwheels;
[Link] = type;
[Link] = price;
}
Car(String brand, String color ,String type)
{
[Link] = brand;
[Link] = color;
[Link] = type;
}
Car(int noofwheels, double price)
{
[Link] = noofwheels;
[Link] = price;
}
Car(String brand, String color, int noofwheels)
{
[Link] = brand;
[Link] = color;
[Link] = noofwheels;
}
Car(String brand, int noofwheels, String color)
{
[Link] = brand;
[Link] = color;
[Link] = noofwheels;
}
// parameterized constructor
Car(String brand, String color, int noofwheels, String type, double price)
{
[Link] = brand;
[Link] = color;
[Link] = noofwheels;
[Link] = type;
[Link] = price;
}
}
Output:
############ CAR DETAILS ###########
The brand of the car is : HYUNDAI
The color of the car is : null
The no of wheels in the car is : 0
The fuel type of the car is : null
The price of the car is : 0.0
####################################
############ CAR DETAILS ###########
The brand of the car is : HYUNDAI
The color of the car is : BLUE
The no of wheels in the car is : 4
The fuel type of the car is : PETROL
The price of the car is : 290000.0
####################################
CONSTRUCTOR CHAINING
*********************
3. Constructor chaining Example
*******************************
package programmesofcorejava;
class ConstrucctorChaining
{
public static void main(String[] args)
{
Cars c1 = new Cars("HYUNDAI","BLUE",4,"PETROL",290000);
// c1 ==> object reference of Car type
[Link](" ############ CAR DETAILS ###########");
[Link](" The brand of the car is : "+[Link]);
[Link](" The color of the car is : "+[Link]);
[Link](" The no of wheels in the car is : "+[Link]);
[Link](" The fuel type of the car is : "+[Link]);
[Link](" The price of the car is : "+[Link]);
[Link](" ####################################");
}
}
class Cars // no of constructors = 3 = (n = 3) ==> (n-1) == > (3-1) = 2 no of
this() can be used
{
String brand;
String color;
int noofwheels;
String type;
double price;
// parameterized constructor
Cars(String brand)
{
[Link] = brand;
}
// parameterized constructor
Cars(String brand, String color)
{
this(brand); // constructor chaining
[Link] = color;
}
// parameterized constructor
Cars(String brand, String color, int noofwheels, String type, double price)
{
this(brand, color); /// constructor chaining
[Link] = noofwheels;
[Link] = type;
[Link] = price;
}
}
Output:
############ CAR DETAILS ###########
The brand of the car is : HYUNDAI
The color of the car is : BLUE
The no of wheels in the car is : 4
The fuel type of the car is : PETROL
The price of the car is : 290000.0
####################################
SCANNER CLASS
*************
4. Scanner class example
************************
package programmesofcorejava;
import [Link]; // import statement
class ScannerClassEx
{
public static void main(String[] args)
{
Scanner s = new Scanner([Link]); // Creating an object to the
scanner class
// s ==> object reference of Scanner type
[Link](" Enter the student name: ");
String name1 = [Link]();
[Link](" Enter the student ID: ");
int id1 = [Link]();
[Link](" Enter the student contact no: ");
long cno1 = [Link]();
[Link](" Enter the percentage secured by the student: ");
float percent1 = [Link]();
Output:
Enter the student name:
ashwini
Enter the student ID:
123
Enter the student contact no:
64646557575
Enter the percentage secured by the student:
89.34