- Java Programming Basics
- Java Tutorial
- Java Environment Setup
- Java Separators
- Java Data Types
- Java Variables
- Java Variable Scope
- Java Type Casting
- Java Operators
- Java Increment Decrement
- Java Left Shift
- Java Right Shift
- Java Bitwise Operators
- Java Ternary Operator
- Java Control Statements
- Java if-else statement
- Java for Loop
- Java while Loop
- Java do-while Loop
- Java switch Statement
- Java break Statement
- Java continue Statement
- Java Popular Topics
- Java Arrays
- Java Multidimensional Array
- Java Strings
- Java Methods
- Java Date and Time
- Java Exception Handling
- Java File Handling
- Java OOP
- Java Classes and Objects
- Java Constructors
- Java Constructor Overloading
- Java Object as Parameter
- Java Returning Objects
- Java Encapsulation
- Java Abstraction
- Java Inheritance
- Java Polymorphism
- Java Packages
- Java Import Statement
- Java Multithreading
- Java Suspend Resume Stop Thread
- Java Programming Examples
- Java Programming Examples
Constructor overloading in Java
Constructors, which are used to create and initialize objects of a class, are a fundamental component of object-oriented programming languages like Java. A class in Java may have several constructors with various parameter lists. Constructor overloading is what enables you to create objects in a variety of ways depending on the particular requirements of your program.
Each constructor accepts a unique set of parameters, enabling the user to create objects with distinct initial values. It is an essential aspect of object-oriented programming that constructor overloading enables flexibility and usability when creating objects.
The general form that shows how constructors can be overloaded in the Java programming language is as follows:
public class ClassName { // Fields and methods of the class // Constructor Overloading public ClassName() { // No-argument constructor } public ClassName(int arg1, int arg2) { // Constructor with two int arguments } public ClassName(int arg1, String arg2) { // Constructor with one int and one String argument } public ClassName(int arg1) { // Constructor with one int argument } public ClassName(int arg1, int arg2, int arg3, int arg4) { // Constructor with four int arguments } }
Java constructor overloading example
Consider the following Java program as an example demonstrating constructor overloading:
public class MyClass { private int num1, num2, num3, num4; private String str; // Constructor Overloading public MyClass() { } public MyClass(int arg1, int arg2) { num1 = arg1; num2 = arg2; } public MyClass(int arg1, String arg2) { num1 = arg1; str = arg2; } public MyClass(int arg1) { num1 = arg1; } public MyClass(int arg1, int arg2, int arg3, int arg4) { num1 = arg1; num2 = arg2; num3 = arg3; num4 = arg4; } // method to print out the values of the fields public void printValues() { System.out.println("num1: " + num1); System.out.println("num2: " + num2); System.out.println("num3: " + num3); System.out.println("num4: " + num4); System.out.println("str: " + str); System.out.println(); } public static void main(String[] args) { MyClass obj1 = new MyClass(); // No-argument constructor MyClass obj2 = new MyClass(10, 20); // Constructor with two int arguments MyClass obj3 = new MyClass(5, "Hello"); // Constructor with one int and one String argument MyClass obj4 = new MyClass(15); // Constructor with one int argument MyClass obj5 = new MyClass(1, 2, 3, 4); // Constructor with four int arguments // Print out the values of the fields for each object obj1.printValues(); obj2.printValues(); obj3.printValues(); obj4.printValues(); obj5.printValues(); } }
num1: 0 num2: 0 num3: 0 num4: 0 str: null num1: 10 num2: 20 num3: 0 num4: 0 str: null num1: 5 num2: 0 num3: 0 num4: 0 str: Hello num1: 15 num2: 0 num3: 0 num4: 0 str: null num1: 1 num2: 2 num3: 3 num4: 4 str: null
The first constructor in the class is the default constructor. It takes no arguments and does not initialize any of the fields in the object.
The second constructor requires two integers as arguments and uses those values to populate the fields "num1" and "num2" in the object it creates.
The third constructor accepts two arguments, a string and an integer, and uses them to set the values of the fields "num1" and "str," respectively.
The fourth constructor only accepts one integer argument, and it uses that value to populate the "num1" field in the object.
The fifth constructor accepts four integers as arguments and then uses those values to initialize the fields labeled "num1," "num2," "num3," and "num4" respectively.
The values of each field in the class are printed out by the "printValues" method in the class. Five "MyClass" objects are created using various constructors in the "main" method. Depending on the constructors used, the objects are initialized with different arguments. The "printValues" method prints the fields of each object.
« Previous Tutorial Next Tutorial »