Wednesday, August 21, 2019

Can we overload constructor in java?

Problem statement:
You are given a code for constructor overloading. What do you think, can we overload the constructor in java?

Ans: Yes.
  1. class A {
  2. A() {
  3. System.out.println("default");
  4. }
  5. A(int id) {
  6. System.out.println("id");
  7. }
  8. A(int id, String name) {
  9. System.out.println("id & name");
  10. }
  11. A(String name, Double salary) {
  12. System.out.println("name & salary");
  13. }
  14. }
  15. public class Sample {
  16. public static void main(String[] args) {
  17. A a = new A();
  18. }
  19. }
Output:
default

No comments:

Post a Comment

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...