Skip to content

Instantly share code, notes, and snippets.

@bikashdaga
Created March 3, 2022 10:46
Show Gist options
  • Select an option

  • Save bikashdaga/c2d9d4bef904b35d9e4bc962c3a7d942 to your computer and use it in GitHub Desktop.

Select an option

Save bikashdaga/c2d9d4bef904b35d9e4bc962c3a7d942 to your computer and use it in GitHub Desktop.

Revisions

  1. bikashdaga revised this gist Mar 3, 2022. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions Java Constructor Overloading
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    Input:

    ```
    public class ConstructorEG {
    String name;
    Integer number;
    @@ -30,12 +29,12 @@ public class ConstructorEG {
    System.out.println(object2.number);
    }
    }
    ```


    Output:
    ```

    Parameterized Constructor for name called
    custom value
    Parameterized Constructor for number called
    123
    ```

  2. bikashdaga created this gist Mar 3, 2022.
    41 changes: 41 additions & 0 deletions Java Constructor Overloading
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    Input:

    ```
    public class ConstructorEG {
    String name;
    Integer number;

    ConstructorEG(){ // default constructor
    System.out.println("Default Constructor called");
    }

    ConstructorEG(String defaultValue){ // parameterised constructor 1
    System.out.println("Parameterized Constructor for name called");
    name = defaultValue;
    }

    ConstructorEG(Integer number){ // parameterised constructor 2
    System.out.println("Parameterized Constructor for number called");
    this.number = number;
    }



    public static void main(String[] args) {
    ConstructorEG object = new ConstructorEG("custom value");
    System.out.println(object.name);


    ConstructorEG object2 = new ConstructorEG(123);
    System.out.println(object2.number);
    }
    }
    ```

    Output:
    ```
    Parameterized Constructor for name called
    custom value
    Parameterized Constructor for number called
    123
    ```