In this post, we will discuss the difference between constructor overloading and method overloading in Java.
Constructor Overloading
- Writing more than 1 constructor in a class with a unique set of arguments is called as Constructor Overloading
- All constructors will have the name of the class
- Overloaded constructor will be executed at the time of instantiating an object
- An overloaded constructor cannot be static as a constructor relates to the creation of an object
- An overloaded constructor cannot be final as constructor is not derived by subclasses it won’t make sense
- An overloaded constructor can be private to prevent using it for instantiating from outside of the class
Method Overloading
- Writing more than one method within a class with a unique set of arguments is called as method overloading
- All methods must share the same name
- An overloaded method if not static can only be called after instantiating the object as per the requirement
- Overloaded method can be static, and it can be accessed without creating an object
- An overloaded method can be final to prevent subclasses to override the method
- An overloaded method can be private to prevent access to call that method outside of the class
Articles related to Constructor in Java:
Constructor in Java
Constructor Calling in Java