What is the difference between methods and constructors?
What is the difference between methods and constructors?
constructors:
-> It is used to build object of class
-> It binds the instance variable to objects
-> It does not return any thing
-> private constructor can be used to prohibit object
creation
-> It get invoked implicitly when we create an object
-> constructor can not have return type
methods:
-> It is part of the class which provides functionality to
its objects
-> It may or may not return
-> methods can be reused to another class
-> method have return type or void if no return type
-> methods can be overloaded to inherited class
Example:
class Test{
public Test(){} // default constructor
public Test(int a){} // parameterized constructor
public void print(){} // method with no return
public String toString(){return "hello";} // method with return
type
}
Get Answers For Free
Most questions answered within 1 hours.