Search code examples
javaconstructordefault-constructor

Why does constructor with arg undefine the default constructor?


Consider -

public class Class_A {

    public void func() {...}

    public void func(int a){...}

All three -

Class_A a = new Class_A(); // legal
a.func(); // legal
a.func(1); // legal

But After constructor with arg like public Class_A (int a){...} is added to Class_A , the default constructor become to be -

Class_A a = new Class_A(); // The constructor Class_A() is undefined

Thats force me to add public Class_A() {/*Do Nothing*/} into Class_A .

Since each class has default constructor , why doesn't both default constructor and constructor with arg can exist together just same func() and func(int a) are ?


Solution

  • it has default constructor unless you define your own constructor, in this case you need to re define default constructor