In Java classes, we have methods that operate on the data.
package constructorexp;
import java.util.Scanner; // for user input
class pizza{
String item;
String size;
/* Default CONSTRUCTOR */
pizza(){
item = "Pizza";
size ="Standard";
}
/* Overloaded CONSTRUCTOR */
pizza(String sze){ // Has a parameter for user value for size of pizza
item = "Pizza";
size = sze; // User value is assigned to the attribute
}
}
public class ConstructorExp {
public static void main(String[] args) {
String Wsize = ""; // for user input for size
Scanner Accept = new Scanner(System.in); // object for user input
System.out.println("Enter Size of Pizza (L-Large,M-Medium, [Enter] for Standard");
Wsize = Accept.nextLine(); //method for String input
/* Display of overloaded user values for LARGE Pizza */
if (Wsize.equals("L")){
pizza op = new pizza("Large"); // object from overloaded constructor
System.out.println("Overloaded Constructor --");
System.out.println("Item type:"+ op.item + ", Size:"+op.size);
}
/* Display of overloaded user values for MEDIUM Pizza */
else if (Wsize.equals("M")){
pizza op = new pizza("Medium"); // object from overloaded constructor
System.out.println("Overloaded Constructor --");
System.out.println("Item type:"+ op.item + ", Size:"+op.size);
}
else{
/* Display of default values by default constructor */
pizza p = new pizza(); // Object from default constructor
System.out.println("Default Constructor --");
System.out.println( "Item type:"+ p.item + ", Size:"+p.size);
}
}
}
Output :
run:
Enter Size of Pizza (L-Large,M-Medium, [Enter] for Standard
<enter>
Default Constructor --
Item type:Pizza, Size:Standard
Enter Size of Pizza (L-Large,M-Medium, [Enter] for Standard
M
Overloaded Constructor --
Item type:Pizza, Size:Medium
Enter Size of Pizza (L-Large,M-Medium, [Enter] for Standard
L
Overloaded Constructor --
Item type:Pizza, Size:Large
Constructor is also a method but with a special function.
Its function is to initialize the class attributes
automatically when an object of the class is created.
Constructor method is not called in the program like normal methods,
it is invoked automatically when an
object of the class is created.
It is called as constructor because it constructs the object
with values after creation.
How we distinguish this special method from other methods in
a class?
Distinction between constructor method and other methods is
in the name of the method.
Constructor method carries the same name as that of the
class, not user defined names.
Can the values set by the constructor be changed ?
They can be changed,
Though constructor initialize the attributes of a class with
default values,
Those values can be
changed by another feature called as OVERLOADED CONSTRUCTOR.
The difference between the default constructor and
overloaded constructor is,
The default constructor simply assigns default values to the attributes of a
class after creation of the object automatically.
It has no parameters
and does not return any values.
Whereas,
The overloaded constructor methods have parameters.
Through arguments, they assign user supplied values to the
attributes of a class, thus changing the values set by the default constructor.
How many overloaded constructor a class can have?
A class can have more than one overloaded constructor
provided they carry different method signature.
Though default constructor and overloaded constructors have
the same name, they should have different method signature.
(Signature of a method constitutes Name, number of arguments
and type of arguments.)
As far as access,
Based on access specifics (public, private, protected),
features are accessible to other classes.
By default they have friendly access – accessible to whole
package.
Example:
We will see a simple example.
A pizza shops sells pizzas of three sizes - Standard,Medium and Large.
Supposing they sell around 90 standard size pizzas out of
every hundred.
Then, they would maintain values of a Standard size pizza as
default values,
so that they need not type detail whenever a standard pizza is ordered.
This will save time, since it constitutes 90 percent of
sales volume.
But, out of every 100 pizza, they also sell 10 pizzas of medium and large
size.
So, they have to overload the default values as well.
The following program illustrates how default and overloaded constructors can be implemented.
Program -
package constructorexp;
import java.util.Scanner; // for user input
class pizza{
String item;
String size;
/* Default CONSTRUCTOR */
pizza(){
item = "Pizza";
size ="Standard";
}
/* Overloaded CONSTRUCTOR */
pizza(String sze){ // Has a parameter for user value for size of pizza
item = "Pizza";
size = sze; // User value is assigned to the attribute
}
}
public class ConstructorExp {
public static void main(String[] args) {
String Wsize = ""; // for user input for size
Scanner Accept = new Scanner(System.in); // object for user input
System.out.println("Enter Size of Pizza (L-Large,M-Medium, [Enter] for Standard");
Wsize = Accept.nextLine(); //method for String input
/* Display of overloaded user values for LARGE Pizza */
if (Wsize.equals("L")){
pizza op = new pizza("Large"); // object from overloaded constructor
System.out.println("Overloaded Constructor --");
System.out.println("Item type:"+ op.item + ", Size:"+op.size);
}
/* Display of overloaded user values for MEDIUM Pizza */
else if (Wsize.equals("M")){
pizza op = new pizza("Medium"); // object from overloaded constructor
System.out.println("Overloaded Constructor --");
System.out.println("Item type:"+ op.item + ", Size:"+op.size);
}
else{
/* Display of default values by default constructor */
pizza p = new pizza(); // Object from default constructor
System.out.println("Default Constructor --");
System.out.println( "Item type:"+ p.item + ", Size:"+p.size);
}
}
}
run:
Enter Size of Pizza (L-Large,M-Medium, [Enter] for Standard
<enter>
Default Constructor --
Item type:Pizza, Size:Standard
Enter Size of Pizza (L-Large,M-Medium, [Enter] for Standard
M
Overloaded Constructor --
Item type:Pizza, Size:Medium
Enter Size of Pizza (L-Large,M-Medium, [Enter] for Standard
L
Overloaded Constructor --
Item type:Pizza, Size:Large
No comments:
Post a Comment