Constructor overloading in enumerations in Java.



Overloading is one of the mechanisms to achieve polymorphism where, a class contains two methods with same name and different parameters. It allows one method to perform different tasks based on the parameters passed to it.

Whenever you call this method, the method body will be bound with the method call based on the parameters we pass to it.

Constructor Overloading in Java

Constructors are special methods that are called when an object is created. Constructor overloading in Java allows a class to have multiple constructors with different parameters. This is useful when you want to create objects in different ways, depending on the parameters passed during object creation, similar to method overloading.

Example

Following is an example of constructor overloading in Java:

public class Shonen {
   String name;
   int episodes;

   // Constructor with name and episodes
   public Shonen(String name, int episodes) {
      this.name = name;
      this.episodes = episodes;
   }

   // Constructor with only name, default episodes
   public Shonen(String name) {
      this.name = name;
      this.episodes = 100;
   }

   // Method to display anime details
   public void display() {
      System.out.println("Anime Name: " + name);
      System.out.println("Episodes: " + episodes);
   }

   public static void main(String[] args) {
      // Creating objects using different constructors
      Shonen anime1 = new Shonen("Naruto", 220);
      Shonen anime2 = new Shonen("One Piece");

      // Displaying anime details
      anime1.display();
      System.out.println();
      anime2.display();
   }
}

Following is the outpuut of the above code:

Anime Name: Naruto
Episodes: 220
Anime Name: One Piece
Episodes: 100

Constructor Overloading in Enumerations

Enum is a special Java type which is used for defining a collection of constants. It is a special data type that enables a variable to be a set of predefined constants. The enum constants are static and final by default.

Enumerations in Java can also have constructors, and you can overload them just like regular classes. This allows you to create enum constants with different parameters. Here's an example of constructor overloading in an enumeration:

Example

Following is an example of constructor overloading in an enumeration:

public enum Day {
   MONDAY("Monday", 1),
   TUESDAY("Tuesday", 2),
   WEDNESDAY("Wednesday", 3),
   THURSDAY("Thursday", 4),
   FRIDAY("Friday", 5),
   SATURDAY("Saturday", 6),
   SUNDAY("Sunday", 7);

   private String name;
   private int dayNumber;

   // Constructor with name and day number
   Day(String name, int dayNumber) {
      this.name = name;
      this.dayNumber = dayNumber;
   }

   // Method to display day details
   public void display() {
      System.out.println("Day: " + name + ", Number: " + dayNumber);
   }

   public static void main(String[] args) {
      // Displaying details of each day
      for (Day day : Day.values()) {
         day.display();
      }
   }
}

When you run the above code, it will output:

Day: Monday, Number: 1
Day: Tuesday, Number: 2
Day: Wednesday, Number: 3
Day: Thursday, Number: 4
Day: Friday, Number: 5
Day: Saturday, Number: 6
Day: Sunday, Number: 7

In conclusion, contructor overloading is possible in Java, both in regular classes and in enumerations. It allows you to create objects with different initial states based on the parameters passed to the constructors.

Updated on: 2025-08-21T12:36:30+05:30

969 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements