What is the difference between constructors and other regular methods? What happens if you do not provide a constructor? Can you call one constructor from another? How do you call the superclass’s constructor?
Constructors:
Constructors must have the same name as the class name and cannot return a value. The constructors
are called only once per creation of an object while regular methods can be called many times.
E.g. for a
Pet.class
public Pet() {} // constructor
Regular Methods:
Regular methods can have any name and can be called any number of times. E.g. for a Pet.class.
public void Pet(){} // regular method has a void return type.
Note: method name is shown starting with an uppercase to differentiate a constructor from a regular method. Better naming
convention is to have a meaningful name starting with a lowercase like:
public void createPet(){} // regular method has a void return type
Comments
Post a Comment