What is a Constructor in java
In Java, a constructor is a special type of method that is used to initialize objects. It is called automatically when an object of a class is created. The purpose of a constructor is to initialize the newly created object. Constructors have the same name as the class they are in and do not have a return type, not even void
.
Constructors are particularly useful for setting the initial state of an object by assigning values to its properties or performing other necessary setup operations. Here are the key points about constructors in Java:
1. Name and Signature:
- Constructors have the same name as the class to which they belong.
- Constructors do not have a return type, not even
void
. - Constructors can have parameters, allowing you to pass values to initialize the object.
2. Automatic Invocation:
- Constructors are automatically called when an object is created using the
new
keyword. - They are used to ensure that the object is properly initialized before it is used.
3. Default Constructor:
- If you don’t provide any constructors in your class, Java automatically provides a default constructor with no parameters.
- If you provide at least one constructor in your class, and you still want to have a no-argument constructor, you need to explicitly define it.
Example of Constructors in Java:
Here’s an example of a class with constructors:
/*
* Author: Zameer Ali
* */
public class Person {
private String name;
private int age;
// Parameterized constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Default constructor (no-argument constructor)
public Person() {
this.name = "Unknown";
this.age = 0;
}
// Getter and setter methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
In this example, the Person
class has two constructors: a parameterized constructor that takes name
and age
as parameters and initializes the object, and a default constructor that initializes the object with default values.
You can create Person
objects using these constructors:
Person person1 = new Person("Alice", 30); // Using parameterized constructor
Person person2 = new Person(); // Using default constructor
Can we override the constructor in java?
In Java, constructors cannot be overridden in the traditional sense like methods. When you define a constructor in a subclass, it does not override the constructor of the superclass. However, there are ways to call constructors of the superclass and reuse their code in the subclass using the super
keyword.
Calling Superclass Constructors:
- Using
super()
to Call Parameterized Constructor of Superclass:
/*
* Author: Zameer Ali
* */
public class Subclass extends Superclass {
public Subclass(int parameter) {
super(parameter); // Calls the parameterized constructor of the superclass
// Subclass-specific code here
}
}
Using super
to Call No-Argument Constructor of Superclass:
/*
* Author: Zameer Ali
* */
public class Subclass extends Superclass {
public Subclass() {
super(); // Calls the no-argument constructor of the superclass
// Subclass-specific code here
}
}
In the examples above, super()
is used to call the constructor of the superclass. This allows you to initialize the fields of the superclass in the subclass constructor.
Remember, if you don’t explicitly call a superclass constructor using super()
, Java will automatically insert a call to the no-argument constructor of the superclass. If the superclass does not have a no-argument constructor (i.e., it only has parameterized constructors), and you don’t provide an explicit call to one of the superclass constructors, the Java compiler will throw an error.
Also, note that constructors are not inherited, so you cannot directly override them. But you can provide your own constructors in the subclass and choose to call appropriate superclass constructors using super()
. This allows you to reuse the initialization logic of the superclass in the subclass constructors.