Computer Applications
State the difference between constructor and method.
Java Constructors
ICSE 2005
24 Likes
Answer
Constructor | Method |
---|---|
It is a block of code that initializes a newly created object. | It is a group of statements that can be called at any point in the program using its name to perform a specific task. |
It has the same name as class name. | It should have a different name than class name. |
It has no return type | It needs a valid return type if it returns a value otherwise void |
It is called implicitly at the time of object creation | It is called explicitly by the programmer by making a method call |
If a constructor is not present, a default constructor is provided by Java | In case of a method, no default method is provided. |
It is not inherited by subclasses. | It may or may not be inherited depending upon its access specifier. |
Answered By
12 Likes