Java Basics : Constructors
A constructor is a special method that is used to initialize an object.Every class has a constructor,if we don't explicitly declare a constructor for any java class the compiler builds a default constructor for that class. A constructor does not have any return type.
A constructor has same name as the class in which it resides. Constructor in Java can not be abstract, static, final or synchronized. These modifiers are not allowed for constructor.
class Car { String name ; String model; Car( ) //Constructor { name =""; model=""; } }
There are two types of Constructor
- Default Constructor
- Parameterized constructor
Each time a new object is created at least one constructor will be invoked.
Car c = new Car() //Default constructor invoked Car c = new Car(name); //Parameterized constructor invoked
Constructor Overloading
Like methods, a constructor can also be overloaded. Overloaded constructors are differentiated on the basis of their type of parameters or number of parameters. Constructor overloading is not much different than method overloading. In case of method overloading you have multiple methods with same name but different signature, whereas in Constructor overloading you have multiple constructor with different signature but only difference is that Constructor doesn't have return type in Java.
Q. Why do we Overload constructors ?
Constructor overloading is done to construct object in different ways.
Example of constructor overloading
class Cricketer { String name; String team; int age; Cricketer () //default constructor. { name =""; team =""; age = 0; } Cricketer(String n, String t, int a) //constructor overloaded { name = n; team = t; age = a; } Cricketer (Cricketer ckt) //constructor similar to copy constructor of c++ { name = ckt.name; team = ckt.team; age = ckt.age; } public String toString() { return "this is " + name + " of "+team; } } Class test: { public static void main (String[] args) { Cricketer c1 = new Cricketer(); Cricketer c2 = new Cricketer("sachin", "India", 32); Cricketer c3 = new Cricketer(c2 ); System.out.println(c2); System.out.println(c3); c1.name = "Virat"; c1.team= "India"; c1.age = 32; System .out. print in (c1); } } output: this is sachin of india this is sachin of india this is virat of india
Frequently asked questions
1. Define Constructor?
- Constructor is a special method given in OOP language for creating and initializing object.
- In java , constructor role is only initializing object , and new keyword role is creating object.
2.What are the Rules in defining a constructor?
- Constructor name should be same as class name.
- It should not contain return type.
- It should not contain Non Access Modifiers: final ,static, abstract, synchronized
- In it logic return statement with value is not allowed.
- It can have all four accessibility modifiers: private , public, protected, default
- It can have parameters
- It can have throws clause: we can throw exception from constructor.
- It can have logic, as part of logic it can have all java legal statement except return statement with value.
- We can not place return in constructor.
3. Can we define a method with same name of class?
- Yes, it is allowed to define a method with same class name. No compile time error and no runtime error is raised, but it is not recommended as per coding standards.
4.If we place return type in constructor prototype will it leads to Error?
- No, because compiler and JVM considers it as a method.
5. How compiler and JVM can differentiate constructor and method definitions of both have same class name?
- By using return type , if there is a return type it is considered as a method else it is considered as constructor.
6. How compiler and JVM can differentiate constructor and method invocations of both have same class name?
- By using new keyword, if new keyword is used in calling then constructor is executed else method is executed.
7.Why return type is not allowed for constructor?
- As there is a possibility to define a method with same class name , return type is not allowed to constructor to differentiate constructor block from method block.
8.Why constructor name is same as class name?
- Every class object is created using the same new keyword , so it must have information about the class to which it must create object .
- For this reason constructor name should be same as class name.
9.Can we declare constructor as private?
- Yes we can declare constructor as private.
- All four access modifiers are allowed to
- constructor.
- We should declare constructor as private for not to allow user to create object from outside of our class.
- Basically we will declare private constructor in Singleton design pattern.
10.Is Constructor definition is mandatory in class?
- No, it is optional . If we do not define a constructor compiler will define a default constructor.
11. Why compiler given constructor is called as default constructor?
- Because it obtain all its default properties from its class.
- They are
1.Its accessibility modifier is same as its class accessibility modifier
2.Its name is same as class name.
3.Its does not have parameters and logic.
12. what is default accessibility modifier of default constructor?
- It is assigned from its class.
13.When compiler provides default constructor?
- Only if there is no explicit constructor defined by developer.
14.When developer must provide constructor explicitly?
- If we want do execute some logic at the time of object creation, that logic may be object initialization logic or some other useful logic.
15.If class has explicit constructor , will it have default constructor?
- No. compiler places default constructor only if there is no explicit constructor.
16.Can abstract class have constructor ?
- Yes, abstract can have constructor for chaining for constructor .when any class extend abstract class, constructor of sub class will invoke constructor of super class either implicitly or explicitly.