Constructor
• Definition: The constructor is a specialized method for initializing objects.
• Name of the constructor is same as that of its class name. In other words, the name of the constructor and class name is same.
• Whenever an object of its associated class is created, the constructor is invoked automatically.
• The constructor is called constructor because it creates values for data fields of class.
• Example -
Java Program[Rectangle1.java]
public class Rectangle 1 {
int height;
int width;
Rectangle1()
{
System.out.println(" Simple constructor: values are initialised...");
height=10;
width=20;
}
Rectangle 1 (int h,int w)
{
System.out.println(" Parameterised constructor: values are initialised...");
height=h;
width=w;
}
void area()
{
System.out.println("Now, The function is called...");
int result height*width;
System.out.println("The area is "+result);
}
class Constr_Demo {
public static void main(String args[])
{
Rectangle1 obj = new Rectangle 1();
obj.area();//call the to method
Rectangle1 obj1 = new Rectangle 1(11,20);
obj1.area();//call the to method
}
}
Output
F:\test>javac Rectangle1.java
F:\test>java Constr_Demo
Simple constructor: values are initialised...
Now, The function is called...
The area is 200
Parameterised constructor: values are initialised...
Now, The function is called...
The area is 220
1. Name of constructor must be the same as the name of the class for which it is being used.
2. The constructor must be declared in the public mode.
3. The constructor gets invoked automatically when an object gets created.
4. The constructor should not have any return type. Even a void data type should not be written for the constructor.
5. The constructor cannot be used as a member of union or structure.
6. The constructors can have default arguments.
7. The constructor cannot be inherited. But the derived class can invoke the constructor of base class.
8. Constructor can make use of new or delete operators for allocating or releasing memory respectively.
9. Constructor can not be virtual.
10. Multiple constructors can be used by the same class.
11. When we declare the constructor explicitly then we must declare the object of that class.
Methods
Methods are nothing but the functions defined by the particular class.
There are four parts of the method -
• Return type of the method
• name of the method
• parameter passed to the method
• body of the method
• Parameter can be passed to the function by two ways-
1. Call by value
2. Call by reference
• In the call by value method the value of the actual argument is assigned to the formal parameter. If any change is made in the formal parameter in the subroutine definition then that change does not reflect the actual parameters.
• Following Java program shows the use of parameter passing by value-
Java Program
//Program implementing the parameter passing by value
public class ParameterByVal
{
void Fun(int a,int b)
{
a=a+5;
b=b+5;
}
public static void main(String args[])
{
ParameterByVal obj1=new ParameterByVal();
int a,b;
a=10;b=20;
System.out.println("The values of a and b before function call");
System.out.println("a= "+a);
System.out.println("b= "+b);
obj1.Fun(a,b);
System.out.println("The values of a and b after function call");
System.out.println("a= "+a);
System.out.println("b= "+b);
}
}
Output
The values of a and b before function call
a= 10
b= 20
The values of a and b after function call
a= 10
b= 20
A) Constructors do not have a return type.
B) Constructors can have a return type.
C) Constructors must have the void return type.
D) Constructors are not allowed in Java classes.
A) A constructor that takes no arguments and initializes instance variables with default values.
B) A constructor that initializes instance variables with specific values.
C) A constructor that is explicitly defined by the user.
D) A constructor that is automatically invoked when a new class is created.
A) public MyClass() {}
B) public MyClass(int x) { this.x = x; }
C) public MyClass() { this.x = 10; }
D) MyClass(int x) { this.x = x; }
A) The class cannot be instantiated.
B) A default constructor is automatically provided by the Java compiler.
C) An error occurs during compilation.
D) The class is considered abstract.
A) Parameterized constructor
B) Default constructor
C) Copy constructor
D) Private constructor