What is a constructor?
Ans)A constructor is also type of method which will be called and executed at the time of
creation of an Object.By using default or zero argumented constructor,always we can initialize
the instance variables with default values.By using parameterized constructor we can initialize
the instance variables with different values.
________________________________________________
Why is Overloading useful in java?
Ans)
Overloading:
Writing two or more methods with the same name and same method signatures but with different
number of parameters in the same class is called overloading .
If we want to extends the available features of the class then we have to use overloading in java.
Like public int add(int x,int y);
public double add(doublex,double y);
here both the methods are performing the addition operation on two numbers.But based on the
type of data that we are passing the corresponding method will be called and getting executed.
If we pass two integer values as parameters then public int add(int x,int y) will be executed (or)
if we pass two double values as parameters then public double add(doublex,double y) method
will be executed.
We can achieve dynamic polymorphism by using method overloading.
//Default constructor
MyClass()
{
System.out.println("Inside MyClass().");
x=0;
}
//Parameterized constructor
//Here we are calling the MyClass(int i) constructor by passing integer type value
MyClass(int i)
{
System.out.println("Inside MyClass(int).");
x=i;
}
//Parameterized constructor
//Here we are calling the MyClass(double d) constructor by passing double type value
MyClass(double d)
{
System.out.println("Inside MyClass(double).");
x=(int) d;
}
//Parameterized constructor
//Here we are calling the MyClass(int i,intj) constructor by passing two integer type //values
MyClass(int i, int j){
System.out.println("Inside MyClass(int,int).");
x=i*j;
}
________________________________________________
What is a parameter?
Ans)Parameter is type of variable that we are passing as input to the method or constructor.
The method or constructor will perform internal operations by receiving the parameter value as
input.
________________________________________________
Does java use pass parameters by reference or by value ?
Ans)
Java will use pass by value.It wont uses pass by reference.Everything it passes by value.When
ever we are calling a method by passing arguments,java interally creates the copy of the variable
inside the original variable and pass those to the method as arguments.This is called pass by
value.There is no pass by reference in java.
_________________________________________
Part2:(Modified according to the requirement)
Override.java
class Override {
void ovlDemo(){
System.out.println("No parameters");
}
//Override ovlDemo for one integer parameter.
void ovlDemo(int a) {
System.out.println("One parameter: " + a);
}
//Override ovlDemo for two integer parameters.
int ovlDemo(int a, int b){
System.out.println("Two parameters: " + a + " " + b);
return a + b;
}
//Overload ride ovlDemo for two double parameters.
double ovlDemo(double a, double b,double c) {
System.out.println("Two double parameters: " + a + " " + b+" "+c);
return a + b + c;
}
}
___________________________________________________
OverrideDemo.java
class OverrideDemo {
public static void main(String args[]) {
Override ob = new Override(); //instantiation of object
int resI;
double resD;
// call all versions of ovlDemo()
ob.ovlDemo();
System.out.println();
ob.ovlDemo(2);
System.out.println();
resI = ob.ovlDemo(4,6);
System.out.println("Result of ob.ovlDemo(4,6): " + resI);
System.out.println();
//Calling the method by passing 3 parameters.
resD = ob.ovlDemo(1.1,2.32,8.89);
//Displaying the result.
System.out.println("Result of ob.ovlDemo(1.1,2.32,8.89): " + resD);
}
}
_________________________________________________
Output:
No parameters
One parameter: 2
Two parameters: 4 6
Result of ob.ovlDemo(4,6): 10
Two double parameters: 1.1 2.32 8.89
Result of ob.ovlDemo(1.1,2.32,8.89): 12.31
_______________________________________________________
Solution
What is a constructor?
Ans)A constructor is also type of method which will be called and executed at the time of
creation of an Object.By using default or zero argumented constructor,always we can initialize
the instance variables with default values.By using parameterized constructor we can initialize
the instance variables with different values.
________________________________________________
Why is Overloading useful in java?
Ans)
Overloading:
Writing two or more methods with the same name and same method signatures but with different
number of parameters in the same class is called overloading .
If we want to extends the available features of the class then we have to use overloading in java.
Like public int add(int x,int y);
public double add(doublex,double y);
here both the methods are performing the addition operation on two numbers.But based on the
type of data that we are passing the corresponding method will be called and getting executed.
If we pass two integer values as parameters then public int add(int x,int y) will be executed (or)
if we pass two double values as parameters then public double add(doublex,double y) method
will be executed.
We can achieve dynamic polymorphism by using method overloading.
//Default constructor
MyClass()
{
System.out.println("Inside MyClass().");
x=0;
}
//Parameterized constructor
//Here we are calling the MyClass(int i) constructor by passing integer type value
MyClass(int i)
{
System.out.println("Inside MyClass(int).");
x=i;
}
//Parameterized constructor
//Here we are calling the MyClass(double d) constructor by passing double type value
MyClass(double d)
{
System.out.println("Inside MyClass(double).");
x=(int) d;
}
//Parameterized constructor
//Here we are calling the MyClass(int i,intj) constructor by passing two integer type //values
MyClass(int i, int j){
System.out.println("Inside MyClass(int,int).");
x=i*j;
}
________________________________________________
What is a parameter?
Ans)Parameter is type of variable that we are passing as input to the method or constructor.
The method or constructor will perform internal operations by receiving the parameter value as
input.
________________________________________________
Does java use pass parameters by reference or by value ?
Ans)
Java will use pass by value.It wont uses pass by reference.Everything it passes by value.When
ever we are calling a method by passing arguments,java interally creates the copy of the variable
inside the original variable and pass those to the method as arguments.This is called pass by
value.There is no pass by reference in java.
_________________________________________
Part2:(Modified according to the requirement)
Override.java
class Override {
void ovlDemo(){
System.out.println("No parameters");
}
//Override ovlDemo for one integer parameter.
void ovlDemo(int a) {
System.out.println("One parameter: " + a);
}
//Override ovlDemo for two integer parameters.
int ovlDemo(int a, int b){
System.out.println("Two parameters: " + a + " " + b);
return a + b;
}
//Overload ride ovlDemo for two double parameters.
double ovlDemo(double a, double b,double c) {
System.out.println("Two double parameters: " + a + " " + b+" "+c);
return a + b + c;
}
}
___________________________________________________
OverrideDemo.java
class OverrideDemo {
public static void main(String args[]) {
Override ob = new Override(); //instantiation of object
int resI;
double resD;
// call all versions of ovlDemo()
ob.ovlDemo();
System.out.println();
ob.ovlDemo(2);
System.out.println();
resI = ob.ovlDemo(4,6);
System.out.println("Result of ob.ovlDemo(4,6): " + resI);
System.out.println();
//Calling the method by passing 3 parameters.
resD = ob.ovlDemo(1.1,2.32,8.89);
//Displaying the result.
System.out.println("Result of ob.ovlDemo(1.1,2.32,8.89): " + resD);
}
}
_________________________________________________
Output:
No parameters
One parameter: 2
Two parameters: 4 6
Result of ob.ovlDemo(4,6): 10
Two double parameters: 1.1 2.32 8.89
Result of ob.ovlDemo(1.1,2.32,8.89): 12.31
_______________________________________________________

What is a constructorAns)A constructor is also type of method whi.pdf

  • 1.
    What is aconstructor? Ans)A constructor is also type of method which will be called and executed at the time of creation of an Object.By using default or zero argumented constructor,always we can initialize the instance variables with default values.By using parameterized constructor we can initialize the instance variables with different values. ________________________________________________ Why is Overloading useful in java? Ans) Overloading: Writing two or more methods with the same name and same method signatures but with different number of parameters in the same class is called overloading . If we want to extends the available features of the class then we have to use overloading in java. Like public int add(int x,int y); public double add(doublex,double y); here both the methods are performing the addition operation on two numbers.But based on the type of data that we are passing the corresponding method will be called and getting executed. If we pass two integer values as parameters then public int add(int x,int y) will be executed (or) if we pass two double values as parameters then public double add(doublex,double y) method will be executed. We can achieve dynamic polymorphism by using method overloading. //Default constructor MyClass() { System.out.println("Inside MyClass()."); x=0; } //Parameterized constructor //Here we are calling the MyClass(int i) constructor by passing integer type value MyClass(int i) { System.out.println("Inside MyClass(int)."); x=i; } //Parameterized constructor //Here we are calling the MyClass(double d) constructor by passing double type value
  • 2.
    MyClass(double d) { System.out.println("Inside MyClass(double)."); x=(int)d; } //Parameterized constructor //Here we are calling the MyClass(int i,intj) constructor by passing two integer type //values MyClass(int i, int j){ System.out.println("Inside MyClass(int,int)."); x=i*j; } ________________________________________________ What is a parameter? Ans)Parameter is type of variable that we are passing as input to the method or constructor. The method or constructor will perform internal operations by receiving the parameter value as input. ________________________________________________ Does java use pass parameters by reference or by value ? Ans) Java will use pass by value.It wont uses pass by reference.Everything it passes by value.When ever we are calling a method by passing arguments,java interally creates the copy of the variable inside the original variable and pass those to the method as arguments.This is called pass by value.There is no pass by reference in java. _________________________________________ Part2:(Modified according to the requirement) Override.java class Override { void ovlDemo(){ System.out.println("No parameters"); } //Override ovlDemo for one integer parameter. void ovlDemo(int a) { System.out.println("One parameter: " + a); }
  • 3.
    //Override ovlDemo fortwo integer parameters. int ovlDemo(int a, int b){ System.out.println("Two parameters: " + a + " " + b); return a + b; } //Overload ride ovlDemo for two double parameters. double ovlDemo(double a, double b,double c) { System.out.println("Two double parameters: " + a + " " + b+" "+c); return a + b + c; } } ___________________________________________________ OverrideDemo.java class OverrideDemo { public static void main(String args[]) { Override ob = new Override(); //instantiation of object int resI; double resD; // call all versions of ovlDemo() ob.ovlDemo(); System.out.println(); ob.ovlDemo(2); System.out.println(); resI = ob.ovlDemo(4,6); System.out.println("Result of ob.ovlDemo(4,6): " + resI); System.out.println(); //Calling the method by passing 3 parameters. resD = ob.ovlDemo(1.1,2.32,8.89); //Displaying the result. System.out.println("Result of ob.ovlDemo(1.1,2.32,8.89): " + resD);
  • 4.
    } } _________________________________________________ Output: No parameters One parameter:2 Two parameters: 4 6 Result of ob.ovlDemo(4,6): 10 Two double parameters: 1.1 2.32 8.89 Result of ob.ovlDemo(1.1,2.32,8.89): 12.31 _______________________________________________________ Solution What is a constructor? Ans)A constructor is also type of method which will be called and executed at the time of creation of an Object.By using default or zero argumented constructor,always we can initialize the instance variables with default values.By using parameterized constructor we can initialize the instance variables with different values. ________________________________________________ Why is Overloading useful in java? Ans) Overloading: Writing two or more methods with the same name and same method signatures but with different number of parameters in the same class is called overloading . If we want to extends the available features of the class then we have to use overloading in java. Like public int add(int x,int y); public double add(doublex,double y); here both the methods are performing the addition operation on two numbers.But based on the type of data that we are passing the corresponding method will be called and getting executed. If we pass two integer values as parameters then public int add(int x,int y) will be executed (or) if we pass two double values as parameters then public double add(doublex,double y) method will be executed. We can achieve dynamic polymorphism by using method overloading. //Default constructor MyClass()
  • 5.
    { System.out.println("Inside MyClass()."); x=0; } //Parameterized constructor //Herewe are calling the MyClass(int i) constructor by passing integer type value MyClass(int i) { System.out.println("Inside MyClass(int)."); x=i; } //Parameterized constructor //Here we are calling the MyClass(double d) constructor by passing double type value MyClass(double d) { System.out.println("Inside MyClass(double)."); x=(int) d; } //Parameterized constructor //Here we are calling the MyClass(int i,intj) constructor by passing two integer type //values MyClass(int i, int j){ System.out.println("Inside MyClass(int,int)."); x=i*j; } ________________________________________________ What is a parameter? Ans)Parameter is type of variable that we are passing as input to the method or constructor. The method or constructor will perform internal operations by receiving the parameter value as input. ________________________________________________ Does java use pass parameters by reference or by value ? Ans) Java will use pass by value.It wont uses pass by reference.Everything it passes by value.When ever we are calling a method by passing arguments,java interally creates the copy of the variable inside the original variable and pass those to the method as arguments.This is called pass by value.There is no pass by reference in java.
  • 6.
    _________________________________________ Part2:(Modified according tothe requirement) Override.java class Override { void ovlDemo(){ System.out.println("No parameters"); } //Override ovlDemo for one integer parameter. void ovlDemo(int a) { System.out.println("One parameter: " + a); } //Override ovlDemo for two integer parameters. int ovlDemo(int a, int b){ System.out.println("Two parameters: " + a + " " + b); return a + b; } //Overload ride ovlDemo for two double parameters. double ovlDemo(double a, double b,double c) { System.out.println("Two double parameters: " + a + " " + b+" "+c); return a + b + c; } } ___________________________________________________ OverrideDemo.java class OverrideDemo { public static void main(String args[]) { Override ob = new Override(); //instantiation of object int resI; double resD; // call all versions of ovlDemo() ob.ovlDemo(); System.out.println();
  • 7.
    ob.ovlDemo(2); System.out.println(); resI = ob.ovlDemo(4,6); System.out.println("Resultof ob.ovlDemo(4,6): " + resI); System.out.println(); //Calling the method by passing 3 parameters. resD = ob.ovlDemo(1.1,2.32,8.89); //Displaying the result. System.out.println("Result of ob.ovlDemo(1.1,2.32,8.89): " + resD); } } _________________________________________________ Output: No parameters One parameter: 2 Two parameters: 4 6 Result of ob.ovlDemo(4,6): 10 Two double parameters: 1.1 2.32 8.89 Result of ob.ovlDemo(1.1,2.32,8.89): 12.31 _______________________________________________________