How to create constructor overloading in java Program

class Language
{
    String name;

    // Constructor method
    Language()
    {
        System.out.println("Constructor method called.");
    }

    // Constructor with parameter
    Language(String t)
    {
        name = t;
    }

    public static void main(String[] args)
    {
        Language cpp = new Language();
        Language java = new Language("Java");

        cpp.setName("C++");

        java.getName();
        cpp.getName();
    }

    // Method to set the name
    void setName(String t)
    {
        name = t;
    }

    // Method to get the name
    void getName()
    {
        System.out.println("Language name: " + name);
    }
}

1. Class Declaration:

public class Language {

   String name;

A class called Language is defined by an instance variable name of type String. This variable will store the name of the programming language.

2. Constructors:

Language() {

   System.out.println(“Constructor method called.”);

}

Language(String t) {

name = t;

}

  • Default Constructor (Language()): It does not accept any arguments, just prints the “Constructor method called.” when the object is declared using this constructor.
  • Parameterized Constructor (Language(String t)): It accepts a String type variable t and it assigns the value to the name variable of the object. So one can set the name variable right after the declaration of the object.

3. Main Method:

public static void main(String[] args) {

   Language cpp = new Language();

   Language java = new Language(“Java”);

   cpp.setName(“C++”);

   java.getName();

   cpp.getName();

}

  • Creating cpp Object via Default Constructor:

Language cpp = new Language(); creates an object of the Language class via default constructor.

The default constructor prints “Constructor method called.” to the console because it gets called at object creation time.

  • Creating java Object via Parameterized Constructor :

Langauge java = new Langauge(“Java”); creates another object of the Langauge class, but this time it uses a parameterized constructor.

The name of the java object is set to  “Java” when the object is created.

  • Setting the name of the cpp Object using setName() Method:

  cpp.setName(“C++”);

invokes the setName() method on the cpp object and sets the name of cpp to  “C++”.

  • Obtaining java Object’s name Using getName() Method:

java.getName(); invokes the getName() method on the java object, which prints “Language name: Java” to the console.

  • Obtaining cpp Object’s name Using getName() Method:

cpp.getName(); invokes the getName() method on the cpp object, which prints “Language name: C++” to the console.

4. Methods:

void setName(String t) {

   name = t;

}

void getName() {

System.out.println(“Language name: ” + name);

}

There are the following methods:

  • setName(String t) : This method sets the name variable of the object to the value passed in the parameter t.
  • getName() : This method prints the current value of the name variable to the console.

Step-by-Step Execution:

1.  Creating cpp Object:

Language cpp = new Language(); This creates a Language object using the default constructor.

The constructor prints  “Constructor method called.”.

2. Creating java Object:

Language java = new Language(“Java”); creates a Language object using the parameterized constructor, setting name to “Java”.

3. Calling setName(“C++”) on cpp Object:

 cpp.setName(“C++”); changes the name of the cpp object to “C++”.

4. Calling getName() on java Object:

java.getName(); prints the name of the java object, which is “Java”.

Output:  “Language name: Java”

5. Calling getName() on cpp Object:

cpp.getName(); prints the name of the cpp object, which is  “C++”.

Output:  “Language name: C++”

Output:

Constructor method called.

Language name: Java

Language name: C++

Summary:

  • Constructor: The program has two constructors: a default constructor and a parameterized constructor. The default constructor prints “Constructor method called.”, and the parameterized constructor sets the name of the object.
  • Methods: There are two methods: setName() (to set the name of an object) and getName() (to print the name of an object).
  • Object Instantiation: There are two objects, namely cpp and java, created: one with the default constructor and the other with the parameterized constructor.
  • Output: It prints the names of the programming languages, which would include the output “Java” and “C++”, using the getName() method.

Leave a Comment

Your email address will not be published. Required fields are marked *