Sunday, 22 February 2015

What are the differences between Contructors and Methods?


 

Constructors
Methods
Purpose
Create an instance of a class
Group Java statements
Modifiers
Cannot be abstract, final, native, static, or synchronized
Can be abstract, final, native, static, or synchronized
Return Type
No return type, not even void
void or a valid return type
Name
Same name as the class (first letter is capitalized by convention) -- usually a noun
Any name except the class. Method names begin with a lowercase letter by convention -- usually the name of an action
this
Refers to another constructor in the same class. If used, it must be the first line of the constructor
Refers to an instance of the owning class. Cannot be used by static methods.
super
Calls the constructor of the parent class. If used, must be the first line of the constructor
Calls an overridden method in the parent class
Inheritance
Constructors are not inherited
Methods are inherited

No comments:

Post a Comment