What is the difference between a constructor and a method in Java?
| Point | Constructor | Method |
|---|---|---|
| Name | Same as the class | Any name |
| Return type | None (not even void) | Required (or void) |
| Called | Automatically with new | By you, with the dot |
| Purpose | Set up the object | Do a job |
| Inherited | No | Yes |
| Default one | Java gives it if you write none | No |
A constructor is not a method and is not inherited, but a subclass constructor calls the parent constructor with super().
Learn more about the similar topics:
| Tutorials |
|---|
| Java Classes and Objects |
| Java Constructors |
| Java static and this Keywords |
| Java Encapsulation and Access Modifiers |
| Java Inheritance |
| Java Polymorphism |
| Java Abstract Classes |
| Exercises & Assignments |
|---|
| Build a BankAccount class |
| Calculate areas with inheritance and polymorphism |
| Use a sealed interface and pattern matching |