Constructor Overloading Chaining Scanner
Constructor Overloading Chaining Scanner
***********************
package programmesofcorejava;
class ConstructorOverloading
{
public static void main(String[] args)
{
Car c = new Car();
// c ==> object reference of Car type
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)
{
this.brand = brand;
this.color = color;
this.noofwheels = noofwheels;
this.type = type;
this.price = 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
####################################
package programmesofcorejava;
class ConstructorOverloading
{
public static void main(String[] args)
{
Car c = new Car("HYUNDAI");
// c ==> object reference of Car type
class Car
{
String brand;
String color;
int noofwheels;
String type;
double price;
// no argument constructor
Car()
{
Car(String brand)
{
this.brand = brand;
}
// parameterized constructor
Car(String brand, String color, int noofwheels, String type, double price)
{
this.brand = brand;
this.color = color;
this.noofwheels = noofwheels;
this.type = type;
this.price = 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
*********************
package programmesofcorejava;
class ConstrucctorChaining
{
public static void main(String[] args)
{
}
}
// parameterized constructor
Cars(String brand)
{
this.brand = brand;
}
// parameterized constructor
Cars(String brand, String color)
{
this(brand); // constructor chaining
this.color = color;
}
// parameterized constructor
Cars(String brand, String color, int noofwheels, String type, double price)
{
this(brand, color); /// constructor chaining
this.noofwheels = noofwheels;
this.type = type;
this.price = price;
}
}
Output:
SCANNER CLASS
*************
package programmesofcorejava;
import java.util.Scanner; // import statement
class ScannerClassEx
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in); // Creating an object to the
scanner class
// s ==> object reference of Scanner type
Output: