0% found this document useful (0 votes)
2 views

Constructor Overloading Chaining Scanner

The document provides examples of constructor overloading and chaining in Java, demonstrating how to create multiple constructors for a class to initialize objects with different parameters. It includes code snippets for creating 'Car' and 'Cars' classes with various constructors and shows how to use the Scanner class to take user input. The outputs illustrate the details of the car objects created and the data collected from user input.

Uploaded by

ashwiniraia07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Constructor Overloading Chaining Scanner

The document provides examples of constructor overloading and chaining in Java, demonstrating how to create multiple constructors for a class to initialize objects with different parameters. It includes code snippets for creating 'Car' and 'Cars' classes with various constructors and shows how to use the Scanner class to take user input. The outputs illustrate the details of the car objects created and the data collected from user input.

Uploaded by

ashwiniraia07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

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

System.out.println(" ############ CAR DETAILS ###########");


System.out.println(" The brand of the car is : "+c.brand);
System.out.println(" The color of the car is : "+c.color);
System.out.println(" The no of wheels in the car is : "+c.noofwheels);
System.out.println(" The fuel type of the car is : "+c.type);
System.out.println(" The price of the car is : "+c.price);
System.out.println(" ####################################");

Car c1 = new Car("HYUNDAI","BLUE",4,"PETROL",290000);


// c1 ==> object reference of Car type

System.out.println(" ############ CAR DETAILS ###########");


System.out.println(" The brand of the car is : "+c1.brand);
System.out.println(" The color of the car is : "+c1.color);
System.out.println(" The no of wheels in the car is : "+c1.noofwheels);
System.out.println(" The fuel type of the car is : "+c1.type);
System.out.println(" The price of the car is : "+c1.price);
System.out.println(" ####################################");
}
}

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
####################################

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

System.out.println(" ############ CAR DETAILS ###########");


System.out.println(" The brand of the car is : "+c.brand);
System.out.println(" The color of the car is : "+c.color);
System.out.println(" The no of wheels in the car is : "+c.noofwheels);
System.out.println(" The fuel type of the car is : "+c.type);
System.out.println(" The price of the car is : "+c.price);
System.out.println(" ####################################");

Car c1 = new Car("HYUNDAI","BLUE",4,"PETROL",290000);


// c1 ==> object reference of Car type

System.out.println(" ############ CAR DETAILS ###########");


System.out.println(" The brand of the car is : "+c1.brand);
System.out.println(" The color of the car is : "+c1.color);
System.out.println(" The no of wheels in the car is : "+c1.noofwheels);
System.out.println(" The fuel type of the car is : "+c1.type);
System.out.println(" The price of the car is : "+c1.price);
System.out.println(" ####################################");
}
}

class Car
{
String brand;
String color;
int noofwheels;
String type;
double price;
// no argument constructor
Car()
{

Car(String brand)
{
this.brand = brand;
}

Car(String brand, String color)


{
this.brand = brand;
this.color = color;
}

Car(int noofwheels, String type, double price)


{
this.noofwheels = noofwheels;
this.type = type;
this.price = price;
}

Car(String brand, String color ,String type)


{
this.brand = brand;
this.color = color;
this.type = type;
}

Car(int noofwheels, double price)


{
this.noofwheels = noofwheels;
this.price = price;
}

Car(String brand, String color, int noofwheels)


{
this.brand = brand;
this.color = color;
this.noofwheels = noofwheels;
}

Car(String brand, int noofwheels, String color)


{
this.brand = brand;
this.color = color;
this.noofwheels = noofwheels;
}

// 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
*********************

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

System.out.println(" ############ CAR DETAILS ###########");


System.out.println(" The brand of the car is : "+c1.brand);
System.out.println(" The color of the car is : "+c1.color);
System.out.println(" The no of wheels in the car is : "+c1.noofwheels);
System.out.println(" The fuel type of the car is : "+c1.type);
System.out.println(" The price of the car is : "+c1.price);
System.out.println(" ####################################");

}
}

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)
{
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:

############ 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 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

System.out.println(" Enter the student name: ");


String name1 = s.next();

System.out.println(" Enter the student ID: ");


int id1 = s.nextInt();

System.out.println(" Enter the student contact no: ");


long cno1 = s.nextLong();
System.out.println(" Enter the percentage secured by the student: ");
float percent1 = s.nextFloat();

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

You might also like