All You Must Know About Java Constructors
In Object Oriented Programming we are playing with classes and objects. If we need to create a Student Management System, then we have to interact with students. We use a constructor to create an object.
What is a constructor?
Constructor is a special method used to create an object. This must be the same as the class name. So as a convention, this is a method which starts with a capital letter. Parameters can be passed to the method.
How to use a constructor?
Let’s write some code for the student registration platform where we save the student's name and age. I have written a simple code as the beginning example.
class StudentRegistration {
public static void main(String[] args) {
Student sam = new Student();
sam.name = "Sam";
sam.age = 24;
System.out.println(sam.getInfo());
}
}
class Student {
String name;
int age;
public String getInfo() {
return this.name + " " + this.age;
}
}
So where is our constructor in the Student class?
In Java, if you do not mention a constructor it will add a default constructor as follows.
class Student {
String name;
int age;
public Student() {}
public String getInfo() {
return this.name + " " + this.age;
}
}
What is the usefulness of having a constructor?
In the above example, we can set the name and age when we create the Sam
object since it is already defined when we create the object. To archive this we can pass the name and age as parameters to the constructor as follows and clean the code.
class StudentRegistration {
public static void main(String[] args) {
Student sam = new Student("Sam", 24);
System.out.println(sam.getInfo());
}
}
class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getInfo() {
return this.name + " " + this.age;
}
}
Note — If you added a constructor then Java is not adding the default constructor and if you try to create an object using Student()
constructor, then it will give an error.
Can we have more than one constructor?
Yes, we can have multiple constructors using method overloading as given in the following example.
class StudentRegistration {
public static void main(String[] args) {
Student sam = new Student("Sam", 24);
System.out.println(sam.getInfo());
Student ann = new Student("Ann");
System.out.println(ann.getInfo());
}
}
class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student(String name) {
this.name = name;
}
public String getInfo() {
return this.name + " " + this.age;
}
}
Constructors' behaviour in child classes
If we create an object using a child class it will call the parent class constructor first and then the child class constructor. Let’s execute the following example and get the result.
class StudentRegistration {
public static void main(String[] args) {
Student sam = new Student();
sam.name = "sam";
sam.age = 24;
System.out.println(sam.getInfo());
}
}
class Person {
String name;
public Person() {
System.out.println("Person");
}
}
class Student extends Person{
int age;
public Student() {
System.out.println("Student");
}
public String getInfo() {
return this.name + " " + this.age;
}
}
In the above example, Student
class is a child class of Person
class. When we create an object using Student
a constructor first it calls the Person
constructor and then executes the rest inside the Student
constrictor. The output is as follows.
Person
Student
sam 24
Let’s polish up the code a bit.
class StudentRegistration {
public static void main(String[] args) {
Student sam = new Student("Sam", 23);
System.out.println(sam.getInfo());
}
}
class Person {
String name;
public Person(String name) {
this.name = name;
}
}
class Student extends Person{
int age;
public Student(String name, int age) {
super(name);
this.age = age;
}
public String getInfo() {
return this.name + " " + this.age;
}
}
We can call the parent class constructor using the super
the keyword as given in the example.
Important
if you have a parent class without having the default constructor. You must call the Parent class constructor. Please check the following code.
class StudentRegistration {
public static void main(String[] args) {
Student sam = new Student("Sam", 23);
System.out.println(sam.getInfo());
}
}
class Person {
String name;
}
class Student extends Person{
int age;
public Student(String name, int age) {
this.age = age;
}
public String getInfo() {
return this.name + " " + this.age;
}
}
The above code will work since we have not defined any constructor for Person
class hence default constructor is set from Java and when we create Student
object it calls the default constructor. But if we have defined a constructor in Person
class which is not the default
constructor. Then you must call the parent class constructor.
class StudentRegistration {
public static void main(String[] args) {
Student sam = new Student("Sam", 23);
System.out.println(sam.getInfo());
}
}
class Person {
String name;
public Person(String name) {
this.name = name;
}
}
class Student extends Person{
int age;
public Student(String name, int age) {
this.age = age; // Give an error
}
public String getInfo() {
return this.name + " " + this.age;
}
}
Hope you got a basic understanding of Constructors in Java. If you have found this helpful please hit that 👏 and share it on social media :)