Constructors and Overloaded constructors.
Constructors and Overloaded constructors.
In Java, constructors are special methods used to initialize objects when they are created. Constructors have the same name as the class they belong to and are called when you create an instance of that class using the new keyword. Overloaded constructors allow you to define multiple constructors for a class with different parameter lists, providing flexibility in object initialization. Let's go over constructors and overloaded constructors in Java.
Constructors
In Java, a constructor is a block of code that resembles a method and is invoked when an instance of a class is being created. During the constructor's execution, memory is allocated for the object in the computer's memory.
Constructors serve as special methods designed for the purpose of initializing objects. When an object is created using the new keyword, at least one constructor is called.
If a class does not explicitly provide any constructors, a default constructor is automatically generated by the Java compiler. This default constructor is invoked when an object is created without specifying any constructor arguments.
Purpose of Constructors:
Constructors are used to set up the initial state of an object.
They can initialize instance variables, allocate resources, and perform any other necessary setup tasks.
Constructors ensure that an object is in a valid and usable state as soon as it is created.
There are two types of constructor
No-Argument Constructor (Default Constructor)
Parameterized Constructor
Default Constructor:
If you don't explicitly define any constructors in your class, Java provides a default constructor with no arguments.
It initializes instance variables to default values (e.g., 0 for numeric types, null for reference types).
Example:
public class MyClass {
// Default constructor
public MyClass() {
// Initialization code here
}
}
Example:-
public class Person {
private String name;
private int age;
// Default constructor
public Person() {
// Initialize attributes with default values
this.name = "Abhay";
this.age = 0;
}
// Getter methods to access private attributes
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class Main {
public static void main(String[] args) {
// Create a Person object using the default constructor
Person person = new Person();
// Access the attributes of the person object
String personName = person.getName();
int personAge = person.getAge();
// Print the values of the attributes
System.out.println("Name: " + personName);
System.out.println("Age: " + personAge);
}
}
Output
Name: Abhay
Age: 0
Parameterized Constructor:
You can define constructors with parameters to initialize instance variables with specific values during object creation.
Example:
public class Person {
private String name;
private int age;
// Parameterized constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
A Java program that demonstrates the use of a parameterized constructor.
public class Person {
private String name;
private int age;
// Parameterized constructor
public Person(String name, int age) {
// Initialize attributes with values provided as parameters
this.name = name;
this.age = age;
}
// Getter methods to access private attributes
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class Main {
public static void main(String[] args) {
// Create a Person object using the parameterized constructor
Person person = new Person("Mishra", 24);
// Access the attributes of the person object
String personName = person.getName();
int personAge = person.getAge();
// Print the values of the attributes
System.out.println("Name: " + personName);
System.out.println("Age: " + personAge);
}
}
Output
Name: Mishra
Age: 24
Overloaded constructors
What Are Overloaded Constructors?:
Overloaded constructors occur when a class has multiple constructors with different parameter lists. They share the same constructor name but differ in the number or types of parameters they accept.
Purpose of Overloaded Constructors:
Overloaded constructors allow you to create objects with different initializations or configurations.
They provide flexibility in how objects are created, as you can choose the constructor that best fits your needs.
public class Person {
private String name;
private int age;
// Constructor with no parameters (default constructor)
public Person() {
name = "Unknown";
age = 0;
}
// Constructor with only the name parameter
public Person(String name) {
this.name = name;
age = 0; // Default age
}
// Constructor with both name and age parameters
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Getter methods for name and age
public String getName() {
return name;
}
public int getAge() {
return age;
}
public static void main(String[] args) {
// Using overloaded constructors to create Person objects
Person person1 = new Person(); // Default constructor
Person person2 = new Person("Alice"); // Constructor with name parameter
Person person3 = new Person("Bob", 30); // Constructor with name and age parameters // Accessing object properties
System.out.println("Person 1: " + person1.getName() + ", Age: " + person1.getAge()); System.out.println("Person 2: " + person2.getName() + ", Age: " + person2.getAge()); System.out.println("Person 3: " + person3.getName() + ", Age: " + person3.getAge());
}
}
Output
Person 1: Unknown, Age: 0
Person 2: Alice, Age: 0
Person 3: Bob, Age: 30
In this example, the Person class defines three constructors:
The default constructor takes no parameters and initializes the name to "Unknown" and age to 0.
The second constructor takes a String parameter for the name and sets the age to a default value of 0.
The third constructor takes both a String parameter for the name and an int parameter for the age.
You can create Person objects using any of these constructors based on your requirements.
Overloaded constructors are a useful feature in Java and other object-oriented programming languages as they provide flexibility when initializing objects of a class, allowing you to create objects with different initial states depending on the constructor you choose to use.